Concerns about using struct initializer in UDA?

Andre Pany via Digitalmars-d digitalmars-d at puremagic.com
Fri May 12 12:01:30 PDT 2017


On Friday, 12 May 2017 at 10:23:34 UTC, Nicholas Wilson wrote:
> On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
>> Hi,
>>
>> I know there are concerns about struct initialization in 
>> method calls but what is about struct initializer in UDA?
>>
>> Scenario: I want to set several UDA values. At the moment I 
>> have to create for each value a structure with exactly 1 
>> field. But it would be quite nice if I could use struct 
>> initialization to group these values:
>>
>> struct Field
>> {
>> 	string location;
>> 	string locationName;
>> }
>>
>> struct Foo
>> {
>> 	@A = {locationName: "B"} int c; // <------------------
>> }
>>
>> void main() {}
>>
>> Of course the syntax is questionable, it is just a proposal.
>> What do you think?
>>
>> Kind regards
>> André
>
> I'm not sure if it isi what you're looking for but you can do
>
>  struct Field
>  {
>  	string location;
>  	string locationName;
>  }
>
>  struct Foo
>  {
>  	@Field("","B") int c;
>  }
>
> We use this for our compiler recognised attributes in LDC, see 
> llvmAttr for an example
> (https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/attributes.d#L108)

I think there are cases where this syntax is more readable and 
there are cases where struct initialization is more readable. In 
the example you gave, I do not know what is the second field 
about and having to mention the first field feels like a burden. 
As developer I want the choice to decide between these 2 syntax 
options.

Kind regards
André


More information about the Digitalmars-d mailing list