struct init property

Namespace rswhite4 at googlemail.com
Thu Aug 23 14:27:18 PDT 2012


> How would it be different from defining a default constructor? 
> Structs
> specifically _don't_ have default constructors, because init 
> must be known at
> compile time, and all kinds of restrictions would have to be 
> placed on a
> default constructor (enough to make it pointless) to ensure 
> that it would work
> as an init value that there's really no point to it. The kind 
> of stuff that
> you'd want to do in a default constructor but can't do by 
> directly
> initializing the member variables is precisely the kind of 
> stuff that you
> _can't_ do with a struct, because init must be known at compile 
> time, must
> always generate the same result, couldn't throw exceptions, etc.
>
> So, sure, at times it would be great to have a default 
> constructor for
> structs, but other design decisions in the language 
> (particularly with regards
> to init) simply make that infeasible. It's one of those "forced 
> faults" in
> language design that Andrei likes to talk about.
>
> So, what we get is an init value defined by how the member 
> variables are
> directly initialized and the ability to define a static opCall 
> to get you the
> equivalent of the default constructor in cases where S() is 
> used rather than
> S.init. It's not perfect, but there's not much that we can do 
> about it.
>
> - Jonathan M Davis

My idea was to declare your own .init in a class, not in a struct.




More information about the Digitalmars-d-learn mailing list