[Issue 9864] New: Allow instantiating eponymous inner template with a single parameter list

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 3 01:03:57 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9864

           Summary: Allow instantiating eponymous inner template with a
                    single parameter list
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-04-03 01:03:55 PDT ---
Here's a hasField template which allows you to instantiate it with only one
parameter to create an alias:

-----
template hasField(string name)
{
    template hasField(T)
    {
        enum bool hasField = __traits(hasMember, T, name);
    }
}

struct S1 { int x; }
struct S2 { int y; }

void main()
{
    alias hasX = hasField!"x";

    static assert(hasX!S1);
    static assert(!hasX!S2);

    static assert(hasField!("x", S1));  // ng
}
-----

Unfortunately we cannot use this template directly. We can turn it into a
normal template:

-----
template hasField(string name, T)
{
    enum bool hasField = __traits(hasMember, T, name);
}
-----

But then we lose the ability to alias the template with a single type
parameter:

-----
alias hasX = hasField!"x";  // ng
-----

There is a library workaround for this, by introducing a Curry template:

-----
template Curry(alias Templ, T...)
{
    template Curry(X...)
    {
        alias Curry = Templ!(T, X);
    }
}

template hasField(string name, T)
{
    enum bool hasField = __traits(hasMember, T, name);
}

struct S1 { int x; }
struct S2 { int y; }

void main()
{
    alias hasX = Curry!(hasField, "x");  // ok

    static assert(hasX!S1);
    static assert(!hasX!S2);

    static assert(hasField!("x", S1));  // ok
}
-----

Nevertheless it would be nice to be able to use the first syntax as well.

I consider this a low priority enhancement since a library workaround exists.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list