Concerns about using struct initializer in UDA?

ag0aep6g via Digitalmars-d digitalmars-d at puremagic.com
Fri May 12 12:33:52 PDT 2017


On 05/12/2017 09:01 PM, Andre Pany wrote:
> On Friday, 12 May 2017 at 10:23:34 UTC, Nicholas Wilson wrote:
[...]
>> 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;
>>  }
[...]
> 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.

You can create the attribute separately:

     enum Field a = { locationName: "B" };
     @a int c;

Or if avoiding the extra symbol is more important than beauty, call a 
function literal:

     @((){ Field a = { locationName: "B" }; return a; }()) int c;

That's not as succinct as the syntax you propose, of course.

But your syntax (`@A = {locationName: "B"} int c;`) misses an important 
detail: There's no indication what the type of the attribute is (or is 
`A` supposed to be the type?). To make it work, you will have to add 
that. Something like `@Field(locationName: "B")` or 
`@Field{locationName: "B"}` or whatever works.

At that point, why limit it to attributes? Constructors like that would 
be nice everywhere. I remember such syntax being discussed repeatedly, 
but I don't know where we stand. Maybe there's a DIP already?


More information about the Digitalmars-d mailing list