no-arg constructor for structs (again)

Maxim Fomin maxim at maxim-fomin.ru
Wed Sep 19 05:32:05 PDT 2012


On Wednesday, 19 September 2012 at 11:51:13 UTC, monarch_dodra 
wrote:
> The biggest issue with not having a no-arg constructor can 
> easilly be seen if you have ever worked with a "Reference 
> Semantic" semantic struct: A struct that has a pointer to a 
> payload. Basically, a class, but without the inherited Object 
> polymorphism.

This means that you still have a class object. What is design 
behind inserting class into the structure for the sake of 
escaping from classes?

> These are hard to work with, both for the user and the 
> implementer: They either use auto-intialization, making EVERY 
> CALL start with "ensure initialied" (costly for ranges). Either 
> that, or they need to be explicitly initialized. Or a mix of 
> both, and a source of bugs and frustration in phobos.

If you know initialize values at compile time, you can use them. 
If not, you can overload opCall to make custom initialization at 
runtime. Yes, it doesn't help to initialize structures which are 
created like "S s;" - but that how structures work: they are 
lightweight objects in some matter of speaking and if somebody 
wants to call some functions even in such cases, he probably 
needs to rethink the design.

> HOWEVER, and in contrast to classes, it is surprising that 
> "auto d = S();" and "auto pd = new S();" does not create an 
> initialized reference semantic struct. It is a bare minimum to 
> give a user the ability to allocate & initialize in a single 
> call...
>

Indeed, they are initialized.

> What is very interesting to note above (IMO), is that the 
> language provides no less than THREE syntaxes to allocate a 
> non-constructed S, two of which can be used with auto:
> *Explicit typing (a)
> *For stack: S.init (c),               parenthesis (d)
> *for new:   without parenthesis (pb), with parenthesis (pd)
>

Which "construction" do you refer?




More information about the Digitalmars-d mailing list