Attributes on Enum Members: Call for use cases.

Timon Gehr timon.gehr at gmx.ch
Thu Nov 30 23:28:47 UTC 2017


On 30.11.2017 15:19, Steven Schveighoffer wrote:
> On 11/30/17 3:26 AM, Timon Gehr wrote:
>> On 30.11.2017 00:23, Steven Schveighoffer wrote:
>>> 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.
> 
> I'm not going to claim authority here, you would know more than me.
> 
> But while it does seem to affect the type, it doesn't seem to affect any 
> of the things you are concerned about:
> 
> int fun(int x) { return 1; }
> pragma(msg, typeof(&fun)); // int function(int x)
> 
> int gun(int y) { return 1; }
> pragma(msg, typeof(&gun)); // int function(int y)
> 
> pragma(msg, is(typeof(&fun) == typeof(&gun))); // true, types are the 
> "same"
> ...

Historical context:
https://issues.dlang.org/show_bug.cgi?id=3866
http://forum.dlang.org/post/mailman.1421.1346020012.31962.digitalmars-d@puremagic.com


> template printType(T)
> {
>      pragma(msg, T); // int function(int) (and only prints once)
>      enum printType = true; // to shut up compiler
> }
> auto x = printType!(typeof(&fun));
> auto y = printType!(typeof(&gun)); // same instantiation
> 
>>> It wouldn't be a giant leap to make this work with attributes as well.
>>>
>>
>> How will this work if typeof(func) does not contain the attributes?
> 
> I guess it has to, in the same way it "contains" the names and 
> everything else. I just didn't realize how it works.
> 
> -Steve

Ok. I think that makes sense. Note that non-user-defined attributes are 
not removed though.


More information about the Digitalmars-d mailing list