Please support shorter constructor init

Nick Treleaven nick at geany.org
Sat Jul 8 12:03:16 UTC 2023


On Saturday, 8 July 2023 at 08:52:29 UTC, FeepingCreature wrote:
> Well, I guess my answer would be "I don't think this is very 
> important, since you can get similarly short constructors using 
> mixins such as this library."

Your mixin doesn't let the constructor also do something else. 
Also you have to import the module with the mixin which is an 
annoyance.

> Honestly, I'd like the language to just generate sensible 
> constructors to begin with. We have default values on struct 
> and class members, so why does the autogenerated constructor 
> allow omitting fields that don't have a default value?

Because you can also omit an initializer for a local variable. If 
you want to restrict field values, define a constructor.

> The `this.x` syntax is very cute, I do like it (Neat uses it 
> for its constructors) but it's sort of "neither here nor 
> there". It's a syntax hack to make it easier to write a 
> constructor that the language could just as easily generate on 
> its own.

Not for `super`.

> Its only advantage is that it lets you reorder constructor 
> parameters, but you can reorder class fields just as easily.

You can't reorder struct fields without changing the layout.



More information about the Digitalmars-d mailing list