2 ms·
I agree that it doesn't really force you to have a null. I found that line on reasoning especially surprising, as Rust also gets around this with sum types and
by hobofan 7y ago
I agree that it doesn't really force you to have a null. I found that line on reasoning especially surprising, as Rust also gets around this with sum types and does have a `Default` trait in the standard library.
I do however think that beyond the empty/one element domains you mentioned, default values can be a bit problematic. An acceptable default value for a type is not really determined by the type itself but in what context it is used.
Imagine a config struct with multiple boolean flags:
struct SomeConfig {
feature_a: bool,
feature_b: bool,
feature_c: bool,
}
Would it be a good idea to use the default value `false` for all of them? I think in a lot of cases like these it is very much preferable to force the programmer to provide values for all the fields.
> If that's the design, we could require the programmer to specify that default literal when the type is defined. Then that literal is used by default construction. Problem solved.
Rust does kind of have a way to do it, though it's a bit more explicit. If you have e.g. `std::default::Default` (which would be the conventional trait for that) implemented, you can easily create a struct without needing to specify the default fields:
let instance = SomeStruct {
foo: 1,
..SomeStruct::default(),
}