derive-new icon indicating copy to clipboard operation
derive-new copied to clipboard

Feature Request: Optional default values

Open luca992 opened this issue 8 months ago • 2 comments

A feature from kotlin I miss:

class Foo(val x: String, val y: String, var z: String = "z")

val foo = Foo("x", "y", "z")
val foo1 = Foo("x", "y")
val foo2 = Foo("x", "y", "z2")

// foo == foo1
// foo != foo2

Would be nice if in rust you could do something similar like:

#[derive(new)]
struct Foo {
    x: String,
    y: String,
    #[new(option, value = "z")]
    z: String,
}

let foo = Foo::new("x", "y", "z");
let foo1 = Foo::new("x", "y", None);
let foo2 = Foo::new("x", "y", Some("z2");

// foo == foo1
// foo != foo2

luca992 avatar Apr 11 '25 20:04 luca992

It baffles my mind this whole crate along with this proposed feature is not already offered by the language itself with a decent syntax.

pycaw avatar Jun 16 '25 10:06 pycaw

@pycaw something like new(z: impl Into<Option<String>>) is valid.

xuxiaocheng0201 avatar Sep 20 '25 17:09 xuxiaocheng0201