Uniform syntax for templates (second try)

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Oct 11 16:28:25 PDT 2008


Marcin Kuszczak wrote:
> This is my second try to propose better syntax for templates. My previous
> proposal was added to bugzilla:
> http://d.puremagic.com/issues/show_bug.cgi?id=1827
> 
> In this proposal I modified a little bit syntax, took into consideration
> more cases and put into table few examples of new syntax compared to old
> one.
> 
> Attached html version of document. Other formats are too big to send it
> through news group, but they are available on request.
> 
> I hope for comments from you :-)

I'll make some candid comments without speaking for Walter.

a) The proposal prevents creating names in expressions, something we 
were talking about:

formattedRead("%s", &(int n));

The sequence "int n" will be ambiguous with a sequence of types.

b) At point 4 my eyes started glazing a bit.

void changeElement(T U N : U[N=uint] if(N>100))
     (T array, U value, int index);

The N=uint jumps as a surprising baroque element. Besides, U[N=uint] 
could be interpreted as a hash with uint as the default key type.

The rewrite in current D2 is:

void changeElement(T : U[N], U, uint N)
     (T array, U value, int index)
     if (N > 100);

To me it's not clear which is better. The second actually may be more 
expressive because you can collectively use all elements of the template 
in one place, something much clunkier in the other version.

c) I totally agree that this should be abolished:

string stringize(T : T[])(T value);

d) Let's try another example.

void parse(T E : E[], U : bool, V E N : E[N] if(N>100))
     (T array, U flag, V sarray);

In D2:

void parse(E, U : bool, V : E[N], uint N)
     (E[] array, U flag, V sarray)
     if(N>100);

e) This will be very hard to understand for both human and machine:

S toupper(isSomeString!(S))(S input)

f) You mention this is not possible, but they are:

void do(T : class)(T instance);
===>
void do(T : Object)(T instance);

void do(T E : E[class])(T sarray, E element);
===>
void do(K, V)(V[K] sarray, K element) if (is(K : Object))

S toupper(isSomeString!(S))(S input)
===>
S toupper(S)(S input) if (isSomeString!(S))

static if (T : Storage!(STORAGETYPE) if (T == MyType))
===>
static if (is(MyType : Storage!(STORAGETYPE), STORAGETYPE)

static if(T == invariant)
===>
static if(T == invariant) ----- should be added

static assert(V E K : E[K] if (K == int), "Not supported");
===>
static assert(is(V : E[int], E), "Not supported");

g) I think this is a rather minor improvement:

alias Variant[][] A B C;

h) I think this should be supported one way or another. Decomposing 
types quickly and easily would be a boon.

alias T U : U[];
alias T U N : U[N=uint] if (N>100);


Andrei



More information about the Digitalmars-d mailing list