Whats going on with this?

Steven Schveighoffer schveiguy at gmail.com
Sat Oct 3 21:25:53 UTC 2020


On 10/3/20 4:46 PM, Daniel Kozak wrote:
> 
> 
> On Sat, Oct 3, 2020 at 10:40 PM Daniel Kozak <kozzi11 at gmail.com 
> <mailto:kozzi11 at gmail.com>> wrote:
> 
>     I would say it is here you just need to read it carefully:
> 
>     https://dlang.org/spec/struct.html#static_struct_init
> 
> 
> For case specification is change I will paste it here:
> '''
> If a StructInitializer is supplied, the fields are initialized by the 
> StructMemberInitializer syntax. StructMemberInitializers with the 
> Identifier : NonVoidInitializer syntax may be appear in any order, where 
> Identifier is the field identifier. StructMemberInitializers with the 
> NonVoidInitializer syntax appear in the lexical order of the fields in 
> the StructDeclaration.
> '''
> 
> And StructMemberInitializer is defined as:
> 
> '''
> StructMemberInitializer:
>      NonVoidInitializer
>      Identifier : NonVoidInitializer
> '''
> 
> And NonVoidInitializer is defined as:
> 
> '''
> NonVoidInitializer:
>      ExpInitializer
>      ArrayInitializer
>      StructInitializer
> '''
> 
> And as you can see there is ArrayInitializer
> 
> And there is definition of Array literals here
> https://dlang.org/spec/expression.html#array_literals
> 
> and in section 2. there is this text:
> 
> '''
> By default, an array literal is typed as a dynamic array, but the 
> element count is known at compile time. So all array literals can be 
> implicitly converted to static array types.
> '''

"StructMemberInitializers with the NonVoidInitializer syntax appear in 
the lexical order of the fields in the StructDeclaration" seems to 
suggest it will not call the constructor, but instead initialize the 
fields according to the list.

The fields are not a static array, so clearly it is calling the 
constructor, and not initializing the fields.

I really don't think this case is in the spec.

But I know it works, for instance:

Variant v = anything;

If this required an explicit Variant constructor, it would be quite 
annoying.

-Steve


More information about the Digitalmars-d-learn mailing list