www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1386] New: "string expected" when using allMembers-element in __traits(getMember, ...)

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386

           Summary: "string expected" when using allMembers-element in
                    __traits(getMember, ...)
           Product: D
           Version: 2.003
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: jascha mainia.de


import std.stdio;

struct Temp(T...)
{
    alias void delegate(T) asdf;
}

class Asdf
{
    Temp!(uint) asdf;
    Temp!(string) qwer;
    Temp!(real,real) yxcv;
}

void main()
{
    foreach ( member; __traits(allMembers, Asdf) )
    {
        Asdf a;
        writefln("%s %s", member, typeid(typeof(__traits(getMember, a,
member))));
    }
}


-- 
Jul 30 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386






__traits works only with strings known at compile time and in your example
'member' is evaluated at run-time. However, allMembers trait evaluates to an
array literal, so there should be a way to iterate through it at compile time.
Unfortunately, D doesn't have true 'static foreach' and you probably have to
resort to hacks.    


-- 
Jul 31 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386






'member' should be evaluated at compile-time, since foreach usually works with
CTFE. if not, the error should be "cannot evaluate ... at compile time".


-- 
Jul 31 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386


mrmocool gmx.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de
           Severity|normal                      |blocker
            Version|2.003                       |2.027




-- 
Apr 01 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386






I've tried everything to work around this, but nothing worked.

In fact this prevents many thinkable uses of compile time introspection.


-- 
Apr 01 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386


dhasenan gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|2.027                       |2.003





Leave the version number on the _earliest_ dmd version exhibiting the problem.


-- 
Apr 01 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386


dhasenan gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|blocker                     |normal





Also, you can work around this using a for-loop (I think) or recursive
templates; and "blocker" refers to a bug sufficiently severe that Walter should
drop everything else and fix this bug, or even possibly roll back to an earlier
version that does not exhibit the bug.


-- 
Apr 01 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386






Ok, sorry didn't know.

Finally managed to compile a workaround:
template Sequence(size_t count, size_t index = 0)
{
        static if (index < count)
                alias Tuple!(index, Sequence!(count, index + 1)) Sequence;  
        else
                alias Tuple!() Sequence;
}

        static const members = __traits (allMembers, foo);
        foreach (i; Sequence!(members.length))
        {
                foreach (p; ParameterTypeTuple!(__traits(getMember, foo,
members[i])))
                        writefln(typeid(p));
        }


-- 
Apr 01 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386


Simen Kjaeraas <simen.kjaras gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras gmail.com



PST ---
As a way to index allMembers as a tuple, here's a template converting an array
known at compile time to a tuple:

template ArrayToTuple( alias Arr, U... ) {
    static if ( Arr.length ) {
        alias ArrayToTuple!( Arr[ 0..$-1 ], Arr[ $-1 ], U ) ArrayToTuple;
    } else {
        alias U ArrayToTuple;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386




allMembers returns a tuple now (svn r360) but this still doesn't work. Yields a
strange error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au



The error message is:
test2.d(22): Error: no property '__T4TempTkZ' for type 'test2.Asdf'
Some kind of junk tuple members are being included in allMembers.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 15 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1386


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



---



But class has a member interface Monitor, so 

 typeid(typeof(__traits(getMember, a, member))))
will fail to compile. If you change `class Asdf` into `struct Asdf`, you'll get following output: asdf test.Temp!(uint).Temp qwer test.Temp!(string).Temp yxcv test.Temp!(real,real).Temp -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 22 2011