www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15634] New: new __traits request: isLvalue

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

          Issue ID: 15634
           Summary: new __traits request: isLvalue
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

If I want a template constraint that requires that some alias/member is an
lvalue, I have to do something very complex (courtesy of Meta from the forums):

template yieldsLval(Aggregate, alias member)
{
    import std.traits: ReturnType;
    import std.functional: FunctionTypeOf;
    import std.typetuple: staticIndexOf;

    static if (isSomeFunction!member)
    {
        enum yieldsLval = __traits(compiles,
        {
            alias Ftype = FunctionTypeOf!member;
            void takesLval(ref ReturnType!Ftype) {}
            takesLval(Ftype());
        });
    }
    //It's a member variable
    else static if (staticIndexOf!(member.stringof, FieldNameTuple!Aggregate) >
-1)
        enum yieldsLval = true;
    else
        static assert(false, "Symbol " ~ member.stringof ~ " is not a member
function or variable of " ~ Aggregate.stringof);
}

struct S
{
    int x;
    ref int y() { return x; }
    int z() { return 1; }
    enum f = 0;
}

void main()
{
    static assert(yieldsLval!(S, S.y));
    static assert(yieldsLval!(S, S.x));
    static assert(!yieldsLval!(S, S.z));
    static assert(!__traits(compiles, yieldsLval!(S, S.f)));
} 

I'm assuming the compiler has this information pretty handy, since it complains
about using rvalues where lvalues are required all the time.

Can we get a __traits call to get this info out of the compiler?

For reference: https://forum.dlang.org/post/n8ls17$2gvd$1 digitalmars.com

--
Feb 01 2016