rust-derivative icon indicating copy to clipboard operation
rust-derivative copied to clipboard

Can default values be specified in some shorter way?

Open vi opened this issue 7 years ago • 6 comments

In #[derivative(Default(value="123"))] boilerplate is ten times bigger than the value itself.

Maybe it can just be sometihng like #[default("123")]? This way it's only five times bigger.

The short form may be opt-out with cargo features to provide a way of dealing with conflicts with other derives.

vi avatar Jan 21 '18 02:01 vi

All attributes must be on the form #[derivative(…)], but we could do something like #[derivative(Default=123)].

mcarton avatar Jan 21 '18 13:01 mcarton

proc_macro_derive(Derivative, attributes(derivative))

Can't there be other, multiple attributes apart from derivative?

vi avatar Jan 23 '18 20:01 vi

All attributes must be on the form #[derivative(…)]

There is no requirement from #[proc_macro_derive] that attributes must be of one form with a specific keyword related to the #[derive]. The question is whether those attributes might conflict with other attributes from other derives.

glandium avatar Jul 13 '18 06:07 glandium

In smart-default it's just #[default].

vi avatar Jul 13 '18 06:07 vi

I don't want just #[default("123")] because that might conflict with other stuffs. Eg. even serde uses #[serde(default = "foo")].

mcarton avatar Oct 20 '18 17:10 mcarton

What if, similar to use foo as bar, you could write this?

#[derive(Derivative)]
#[derivative(Default as bar)]
pub struct Foo {
	#[bar("123")]
	a: u32,
}

This way it's up to the user to avoid name conflicts and in the same way name conflicts are avoided in other contexts.

kankri avatar Aug 21 '19 21:08 kankri