Re:Perils of Constructors
by ape4 on Saturday July 27, 2019 @12:00PM
(#20542605)
Constructors can definitely be misused. But they can be handy too. What if the initial values of the members are just constants. Perhaps it takes a bit of computing (not too much) to set them up.
[ Parent
| on HN ]
> What if the initial values of the members are just constants
In idiomatic rust
impl Thing {
fn new() -> Self {
Thing { ival: 13, fval: 42.0 }
}
Another option is to use Default::default if appropriate.
Re:Perils of Constructors
by davesmith1983 on Saturday July 27, 2019 @12:31PM
(#20542744)
C# has `readonly` variables. The values of these cannot be changed after the constructor has finished running.
Re:Perils of Constructors
by mattnewport on Saturday July 27, 2019 @02:52PM
(#20543494)
I don't find this that useful in C# since the majority of types in C# tend to be reference types and you can still change the values unless they are immutable types which though considered good practice aren't that common in most C# code in my experience.