Tidy template instantiation syntax

KennyTM~ kennytm at gmail.com
Mon Nov 29 14:03:47 PST 2010


On Nov 30, 10 05:20, bearophile wrote:
> 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

-1. The programmer should be able to look up the operator precedence 
when they encounter code like this. This proposal is like requiring

    *p+1

to become a syntax error because it could be one of

    (*p)+1
    *(p+1)

I don't believe this is a common and hard-to-catch source of bugs, so 
the compiler doesn't need to be a style-checker here.


More information about the Digitalmars-d mailing list