Some more template syntax sugar

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Tue Aug 29 03:24:28 PDT 2006


Walter Bright wrote:
> I know. The problem is that it is indistinguishable from:
> 
>     typedef int x;
>     auto sqr(x);
> 
> i.e. when x is a typedef and no parameter name is given, or when x is a 
> parameter name and no type is given.

Is the solution to this any worse than the current spec's dealing with 
aliases.

 From the spec:
>  Note: Type aliases can sometimes look indistinguishable from alias declarations:
> 
> alias foo.bar abc;	// is it a type or a symbol?
> 
> The distinction is made in the semantic analysis pass.
> 
> Aliases cannot be used for expressions:
> 
> struct S { static int i; }
> S s;
> 
> alias s.i a;	// illegal, s.i is an expression
> alias S.i b;	// ok
> b = 4;		// sets S.i to 4

If the semantic analysis pass can determine the validity of alias 
expressions, I would have thought that it could determine the meaning of 
your example:
- if x is defined as a type, then it is a nameless parameter
- if not, then it is a templated function
Of course, this provides no way of shadowing types, but I'm not sure how 
much of a problem that is.

The alternatives I see aren't as nice, since some syntax modification 
would be required:

1. Modify the syntax I propose to make it clear that it's a nameless 
parameter. Basically any additional keyword/symbol could work, but that 
extra character makes a big difference in use. I think

auto sqr(x) { ... }

is much nicer than

auto sqr(@x) { ... }

2. Modify the syntax for nameless parameters. Perhaps something like the 
functional languages, where _ denotes a nameless parameter:

typedef int a;
interface Foo { int bar(a); }

would then become:

typedef int a;
interface Foo { int bar(a _); }

I like this proposal a lot more, because it follows the functional 
language idea of _ being a variable eater. But this is going to break 
much more code, so I'm stuck here.

Cheers,

Reiner



More information about the Digitalmars-d mailing list