Attributes on Enum Members: Call for use cases.
Steven Schveighoffer
schveiguy at yahoo.com
Thu Nov 30 14:19:36 UTC 2017
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"
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
More information about the Digitalmars-d
mailing list