Bug 12411 – New eponymous template syntax could support nested eponymous templates
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-19T01:42:34Z
Last change time
2022-07-04T17:40:04Z
Assigned to
No Owner
Creator
Andrej Mitrovic
Comments
Comment #0 by andrej.mitrovich — 2014-03-19T01:42:34Z
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 by andrej.mitrovich — 2022-07-04T17:40:04Z
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.