<div class="gmail_quote">On 28 March 2012 07:29, F i L <span dir="ltr"><<a href="mailto:witte2008@gmail.com">witte2008@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I find myself using __traits(hasMember, T, "member") a lot.<br>
It's really basic feature of meta-programming.<br>
<br>
I'm not a fan of the "__usedByCompiler" syntax, but that's a<br>
whole different discussion. __traits() is fine (besides the fact<br>
that first-argument-as-function is syntactically inconsistent to<br>
the rest of the language), but std.traits doesn't wrap it<br>
correctly; and, because it's globally available anyways & so<br>
often used, shouldn't have to be imported to use in the first<br>
place, IMO.<br>
<br>
What I would like to see happen: Have a traits.d which replaces<br>
std.traits and is always included (like object.d) which properly<br>
wraps __trait functions.<br>
<br>
so in traits.d:<br>
<br>
     // use alias T, so we can pass "laser.x" to it<br>
     // without having to pass typeof(laser.x). Just like<br>
     // we can with using __traits(hasMember, ...)<br>
<br>
     template hasMember(alias T, string member) {<br>
         enum hasMember = __traits(hasMember, T, member);<br>
     }<br>
<br>
we can then write:<br>
<br>
     auto laser = ship.fire();<br>
     static if(traits.hasMember!(laser.x, "isProp") ...;<br>
<br>
which feels syntactically consistent. Because traits are in their<br>
own module, we could emit the "traits." and just call<br>
"hasMember()". Even better, if UFCS allows, we could simply write:<br>
<br>
     static if(laser.x.hasMember!("isProp"<u></u>)) ...;<br>
<br>
<br>
my 2 cents.<br>
</blockquote></div><br><div>I was rummaging through exactly the same stuff yesterday, and reached a similar conclusion.</div><div>My code became borderline impossible to understand very fast with all the references to __traits(getMember, instanceOf, member) and friends. Something like you suggest would go a long way to making it readable again.</div>
<div><br></div><div>One thing I noticed recurring, __traits(getMember, T, member) can be aliased, but __traits(getMember, instanceOfT, member) can not really be aliased, since D doesn't have references. Is there a practical way to do this? Granted, it's not so necessary if the syntax to do that wasn't so long and cumbersome.</div>
<div><br></div><div>The other thing I found was within a few functions I was desperately wanting user attributes. I was interacting with a database, and without being able to attribute row structure items, it's very messy and hacky to specify which item was the primary key, which items should be relational references, mask items which should be ignored, etc.</div>