Edit: the intent behind this thread was to flesh out enough of the scope to be able to write an RFC, and to that end it was wildly successful. I will be opening an RFC soon. This first post is now slightly divorced from my current thinking (the intent is the same, but the details have changed quite a bit), but I have left a comment further down-thread with a summary of my intentions for the RFC.
I have interacted with some verbose types that are designed to leverage a Default
implementation heavily, foregoing the builder pattern. This can look incredibly verbose:
Foo {
bar: [Bar {
qux: [Qux {
zap: [Zap {
val: "val",
..Default::default()
}],
..Default::default()
}],
..Default::default()
}],
..Default::default()
}
I would like to propose the introduction of some syntactic sugar, not dissimilar to ?
, for Default::default()
. There are multiple alternatives for what this could be, but in my mind it could be the sequence ..*
(which shouldn't introduce ambiguity in the grammar, even though it would introduce some ambiguity when teaching the language):
Foo {
bar: [Bar {
qux: [Qux {
zap: [Zap {
val: "val",
..*
}],
..*
}],
..*
}],
..*
}
Another alternative would be to make default
a contextual keyword (Foo { ..default }
) or using another sigil or keyword.
Have people encountered a need for this? I believe that introducing this in isolation might not be worth it, unless other features complement it. In my mind, if anonymous structs in fn
argument position are also incorporated in the language, then that would be in effect "named arguments" and having this sugar will also help use these two features as a way of getting "optional fn
arguments":
fn foo(_ { bar: Option<usize>, baz: Option<usize> }) {}
fn main() {
foo(_ { bar: Some(3), ..* });
}
What do people think?