Tidy template instantiation syntax

bearophile bearophileHUGS at lycos.com
Mon Nov 29 13:20:14 PST 2010


D has removed some cases of bug-prone C syntax, like:

int main() {
    int* ptr1, ptr2;
    return 0;
}


But D needs to avoid introducing new ones. In D2 there is a shortcut to instantiate templates, but it must not cause ambiguity for the eyes of the programmer that reads the code (the point is not about syntax well defined for a compiler, but about syntax that causes no troubles for mammals. The same is true for the bug-prone C syntax).

So to avoid troubles this code raises a syntax error:

struct Foo(T) {}
Foo!Foo!int y;
void main() {}

test.d(2): multiple ! arguments are not allowed


But this code may look ambiguous for human programmers that read unfamiliar code:

struct Foo(T) {}
Foo!int* x;
static assert(!is(typeof(x) == Foo!(int*)));
static assert(is(typeof(x) == Foo!(int)*));
void main() {}


So in such cases I'd like D to require parentheses, so this becomes a syntax error:
Foo!int* x;

And you need to write:
Foo!(int*)

or:
Foo!(int)*

This avoids a possible source of confusion.

The relative enhancement request:
http://d.puremagic.com/issues/show_bug.cgi?id=5286

Bye,
bearophile


More information about the Digitalmars-d mailing list