Attributes on Enum Members: Call for use cases.

Timon Gehr timon.gehr at gmx.ch
Thu Nov 30 08:26:56 UTC 2017


On 30.11.2017 00:23, Steven Schveighoffer wrote:
> On 11/29/17 4:46 PM, Timon Gehr wrote:
>> On 29.11.2017 20:49, Steven Schveighoffer wrote:
>>> On 11/29/17 2:01 PM, Timon Gehr wrote:
>>>
>>> OK, now I get what you are saying. In the same vein, I tested 
>>> applying attributes to functions themselves:
>>>
>>> @("a") int fun() { return 0; }
>>> pragma(msg, typeof(&fun)); // int function()
>>>
>>> @("b") int gun() { return 1; }
>>> pragma(msg, typeof(&gun)); // int function()
>>>
>>> void main()
>>> {
>>>    auto f = &fun;
>>>    f = &gun; // works
>>> }
>>>
>>> Given that, it seems attributes play no part in the type itself, they 
>>> are just metadata in the compiler.
>>
>> Well, I think the case of UDAs on parameters is closer to:
>>
>> struct S{
>>      @("a") int x1;
>>      @("b") int x2;
>> }
>>
>> The attributes on x1 and x2 are indeed part of the type S, even if the 
>> attributes on S itself are not.
> 
> Right, but unlike functions, you can't make another "overload" of a struct.
> ...

You also cannot make another overload of a function type.

>>
>> If we go with "UDAs on parameters are not part of the function type", 
>> how to you get the UDAs of the parameters of some function?
> 
> Looking at std.traits, it looks like we use this mechanism to get 
> everything:
> 
> int func(int param1)
> 
> static if(is(typeof(func) F == __parameters))
> {
>      static assert(is(typeof(F[0]) == int));
>      static assert(__traits(identifier, F[0]) == "param1");
> }
> 
> This is how vibe.d associates the function-level attributes with the 
> name of the parameter.

This works because the name of the parameter is part of the function 
type. Default arguments are in there, too.

> ...
> It wouldn't be a giant leap to make this work with attributes as well.
> 
> -Steve

How will this work if typeof(func) does not contain the attributes?



More information about the Digitalmars-d mailing list