D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12411 - New eponymous template syntax could support nested eponymous templates
Summary: New eponymous template syntax could support nested eponymous templates
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-19 01:42 UTC by Andrej Mitrovic
Modified: 2022-07-04 17:40 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2014-03-19 01:42:34 UTC
Example code:

-----
import std.typetuple;

enum canAppend(R) = anySatisfy!(canAppendRange!R, Types);

private alias Types = TypeTuple!(int, float);

// problem: can't use new-style eponymous syntax for nexted templates
private template canAppendRange(R)
{
    enum canAppendRange(T) = is(typeof({ R r = void; T t = void; r ~= t; }));
}

void main()
{
    static assert(canAppend!(int[]));
    static assert(canAppend!(float[]));
    static assert(!canAppend!(byte[]));
}
-----

Notice how 'canAppendRange' has to use the old-style eponymous syntax in order for it to be usable with partial instantiation in the call to anySatisfy.

Perhaps a somewhat reasonable enhancement would be to allow nested eponymous syntax:

-----
enum canAppendRange(R)(T) = is(typeof({ R r = void; T t = void; r ~= t; }));
-----

A simplified example test-case:

-----
// old-style
template isEqual(T1)
{
    template isEqual(T2)
    {
        enum bool isEqual = is(T1 == T2);
    }
}

// new-style
enum isEqual(T1)(T2) = is(T1 == T2);

void main()
{
    alias isInt = isEqual!int;
    static assert(isInt!int);
}
-----

The whole idea is to allow new syntax to allow easier writing of templates which can be partially instantiated.
Comment 1 Andrej Mitrovic 2022-07-04 17:40:04 UTC
Can just use this now:

-----
enum isEqual(T1, T2) = is(T1 == T2);

void main()
{
    alias isInt = isEqual!int;
    static assert(isInt!int);
}
-----

It works fine.