[Issue 7364] Better Eponymous Template syntax

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 17 20:58:41 PST 2013


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


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei at erdani.com,
                   |                            |andrej.mitrovich at gmail.com


--- Comment #3 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-02-17 20:58:38 PST ---
Sample code to go with the request:

fullyQualifiedNameImplForTypes from std.traits is currently (excerpt from
code):

----
static if (is(T == string))
{
    enum fullyQualifiedNameImplForTypes = "string";
}
else static if (is(T == wstring))
{
    enum fullyQualifiedNameImplForTypes = "wstring";
}
else static if (is(T == dstring))
{
    enum fullyQualifiedNameImplForTypes = "dstring";
}
else static if (isBasicType!T || is(T == enum))
{
    enum fullyQualifiedNameImplForTypes = chain!((Unqual!T).stringof);
}
else static if (isAggregateType!T)
{
    enum fullyQualifiedNameImplForTypes =
chain!(fullyQualifiedNameImplForSymbols!T);
}
else static if (isStaticArray!T) { ... }
----

With an enhancement it could be turned into:

----
static if (is(T == string))
{
    enum this = "string";
}
else static if (is(T == wstring))
{
    enum this = "wstring";
}
else static if (is(T == dstring))
{
    enum this = "dstring";
}
else static if (isBasicType!T || is(T == enum))
{
    enum this = chain!((Unqual!T).stringof);
}
else static if (isAggregateType!T)
{
    enum this = chain!(fullyQualifiedNameImplForSymbols!T);
}
----

And for an alias example, current code:

----
template TransitiveBaseTypeTuple(T)
{
    static if (is(T == Object))
        alias TypeTuple!() TransitiveBaseTypeTuple;
    else
        alias TypeTuple!(BaseClassesTuple!T, InterfacesTuple!T)
            TransitiveBaseTypeTuple;
}
----

With the enhancement:

----
template TransitiveBaseTypeTuple(T)
{
    static if (is(T == Object))
        alias this = TypeTuple!();
    else
        alias this = TypeTuple!(BaseClassesTuple!T, InterfacesTuple!T);
}
----

The main benefit isn't less typing but less error-prone metaprogramming. It is
very easy to introduce a typo in a template which doesn't instantly trigger a
compile-time error. Even if it does result in a compile-time error it is hard
to figure out what went wrong, for example:

----
template SomeEponymousTemplate(T)
{
    static if (is(T == int))
        alias SomeEponymousTemplate = int;
    else
        alias SomeEponymuosTemplate = float;  // note the typo
}

void main()
{
    alias Type = SomeEponymousTemplate!float;
    Type x;  // line 25
}
----

test(25): Error: template instance test.SomeEponymousTemplate!(float) is used
as a type

Note how the error appears when the type is used, not when the template is
instantiated. I have had these instances of typos quote often in my
metaprogramming code. I could resort to more vigorous copy-pasting, but this is
a bad antipattern. Allowing 'this' in templates would be a an improvement.

I'd like to hear from our BDFL's, perhaps we'll get a pre-approved tag if they
agree.

-- 
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