I've created a new bug from this thread: http://d.puremagic.com/issues/show_bug.cgi?id=9065
That thread has forked into unrelated conversations, and I suspect this is a separate bug anyway...
Kenji: I boiled this down as much as I could...
class Test
{
void func()
{
void templateFunc( T )( ref const T obj )
{
foreach( m; __traits( allMembers, T ) )
{
pragma(msg, m);
static if( isVariable!( __traits(getMember, T, m) ) )
{
//...
}
}
}
templateFunc( this );
}
// some class members
int x;
}
isVariable throws lots of errors when considering the class members.
Note:
__traits(allMembers, T) and __traits(getMember, T, m)
These should also work with class instances, not just types, eg:
__traits(allMembers, obj) and __traits(getMember, obj, m)
And these combinations should also work:
__traits(allMembers, T) and __traits(getMember, obj, m)
__traits(allMembers, obj) and __traits(getMember, T, m)
All these configurations throw errors, and the errors are different for each
configuration.
Depends on:
/**
* Detect whether symbol or type $(D X) is a function.
*/
template isFunction(X...) if (X.length == 1)
{
static if (is(typeof(&X[0]) U : U*) && is(U == function) ||
is(typeof(&X[0]) U == delegate))
{
// x is a (nested) function symbol.
enum isFunction = true;
}
else static if (is(X[0] T))
{
// x is a type. Take the type of it and examine.
enum isFunction = is(T == function);
}
else
enum isFunction = false;
}
/**
* Detect whether symbol $(D X) is a run-time variable.
*/
template isVariable(X...) if (X.length == 1)
{
static if (!is(X[0]) &&
!is(typeof(X[0]) == void) &&
!isFunction!(X[0]))
{
enum isVariable =
is(typeof({ auto ptr = &X[0]; }))
|| is(typeof({ enum off = X[0].offsetof; }));
}
else
enum isVariable = false;
}
Comment #1 by turkeyman — 2012-11-29T04:20:58Z
I have a hard deadline (like, proper hard) tomorrow, I'd really like to have this fix in there. Is there anyone who can possibly look into/comment on it?
I bumped the priority to help it get noticed...
Comment #2 by k.hara.pg — 2012-11-30T20:04:31Z
Furthermore reduced case:
template isVariable(X...) if (X.length == 1)
{ enum isVariable = true; }
class C
{
int x;
void func()
{
enum is_x = isVariable!(__traits(getMember, C, "x"));
}
}
I posted a report bug 9100 to explain the current semantic analysis weird behavior on template argument. I think that it is the root cause of this bug.
(In reply to comment #6)
> Fixed?
Kenji was working on these, the prototypes he gave me worked for me, I'm not sure if it was accepted/merged though. @Kenji?