On Wednesday, 5 October 2016 at 02:15:13 UTC, Jonathan M Davis wrote:
On Wednesday, October 05, 2016 11:20:44 Manu via Digitalmars-d wrote:
> While you're at it, might I suggest also adding > std.traits.isProperty?
>
>  Something like:
> template isProperty(T, string member)
> {
>
>   import std.meta : AliasSeq;
>   import std.traits : FunctionTypeOf;
>   alias sym = AliasSeq!(__traits(getMember, T, member))[0];
>   enum isProperty = !is(typeof(sym) == function) &&
>
> is(FunctionTypeOf!(typeof(&sym)) == function);
> }

Why this AliasSeq business? That line is rather an abomination... why are all these horrible expressions popping up as recommendations nowadays?

The AliasSeq muck is there because for some reason you can't alias the result of __traits, so doing something like

alias sym = __traits(getMember, T, member);

isn't legal. So, this has nothing to do with a recommendation of best practice and everything to do with an annoying bug.

https://issues.dlang.org/show_bug.cgi?id=7804

It _is_ however recommended to use __traits(getMember, T, member) over manually building it with strings with something like T.stringof ~ "." ~ member, because the string manipulation falls about in corner cases (e.g. it could result in a symbol conflict). It's just that then has the unfortunate side effect of requiring AliasSeq because of the bug.

- Jonathan M Davis

https://dlang.org/phobos/std_meta.html#.Alias   :)

Reply via email to