Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 17, 2011 Using __traits(getMember...) in alias statement | ||||
---|---|---|---|---|
| ||||
It would be cool, if the following would be possible. ---- immutable string MemberID = "M"; struct A {} struct B { alias A M; } template Member(T) { static if(__traits(hasMember, T, MemberID)) { alias __traits(getMember, T, MemberID) Member; } else alias TypeTuple!() Member; } void main() { alias Member!(B) M; } ---- Currently dmd will print "basic type expected, not __traits". Why isn't traits allowed here and if we allow __traits there, would this introduce any ambiguities? Tobias |
November 17, 2011 Re: Using __traits(getMember...) in alias statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On 11/17/2011 06:12 PM, Tobias Pankrath wrote:
> It would be cool, if the following would be possible.
> ----
> immutable string MemberID = "M";
>
> struct A {}
> struct B {
> alias A M;
> }
>
> template Member(T)
> {
> static if(__traits(hasMember, T, MemberID))
> {
> alias __traits(getMember, T, MemberID) Member;
> }
> else
> alias TypeTuple!() Member;
> }
>
> void main()
> {
> alias Member!(B) M;
> }
> ----
>
> Currently dmd will print "basic type expected, not __traits".
> Why isn't traits allowed here and if we allow __traits there, would this
> introduce any ambiguities?
>
This works:
alias TypeTuple!(__traits(getMember, T, MemberID)) Member;
It is a mere grammar issue. The aliased symbol must look like a valid type. I'd like that to change too.
This helps a lot with the current state of affairs:
template ID(alias x){alias x ID;}
It will even allow funny things like this:
alias ID!((a,b){return a+b;}) add;
static assert(add(1,2) == 3);
|
November 17, 2011 Re: Using __traits(getMember...) in alias statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | > This helps a lot with the current state of affairs:
>
> template ID(alias x){alias x ID;}
>
> It will even allow funny things like this:
>
> alias ID!((a,b){return a+b;}) add;
>
> static assert(add(1,2) == 3);
Nice to know, thanks!
|
November 17, 2011 Re: Using __traits(getMember...) in alias statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 11/17/2011 06:41 PM, Timon Gehr wrote: > On 11/17/2011 06:12 PM, Tobias Pankrath wrote: >> It would be cool, if the following would be possible. >> ---- >> immutable string MemberID = "M"; >> >> struct A {} >> struct B { >> alias A M; >> } >> >> template Member(T) >> { >> static if(__traits(hasMember, T, MemberID)) >> { >> alias __traits(getMember, T, MemberID) Member; >> } >> else >> alias TypeTuple!() Member; >> } >> >> void main() >> { >> alias Member!(B) M; >> } >> ---- >> >> Currently dmd will print "basic type expected, not __traits". >> Why isn't traits allowed here and if we allow __traits there, would this >> introduce any ambiguities? >> > > This works: > > alias TypeTuple!(__traits(getMember, T, MemberID)) Member; This only works if the __traits gets you a TypeTuple back. (It won't work in this case, it will create a one-element tuple). Better use ID from below. > > It is a mere grammar issue. The aliased symbol must look like a valid > type. I'd like that to change too. > > This helps a lot with the current state of affairs: > > template ID(alias x){alias x ID;} > > > It will even allow funny things like this: > > alias ID!((a,b){return a+b;}) add; > > static assert(add(1,2) == 3); |
Copyright © 1999-2021 by the D Language Foundation