proposed syntax for tuple: t{} and TypeTuple: T{} (cf precedent of q{...})

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 5 08:29:06 PDT 2016


On Tuesday, 5 April 2016 at 05:45:08 UTC, Timothee Cour wrote:
> {} for tuples hasn't worked out since it was deemed ambiguous 
> with delegate syntax (was it for the case of empty 
> statements/tuple?).
>
> How about the following syntax instead:
>
> ----
> {} // delegate (existing syntax)
> q{...} // string literal (existing syntax)
> t{...} // tuple(a,b) (proposed syntax)
> T{...} // TypeTuple!(a,b) (proposed syntax)
> ----
>
> See also [1] where i propose i{...} for a D analog of C++11's 
> uniform initialization.
>
>
> [1] EMAIL: uniform initialization in D (as in C++11): i{...}

I don't think that t or T is needed in front of {}.

1) Function literals / lambdas / delegates:
If someone wants an empty function literal, they just have to use 
(){} instead of {}:
alias Action = void delegate();
Action action = (){}; // instead of
Action action = {}; // error: `{}` is an empty tuple, can't be 
assigned to delegates
auto t = {}; // deduced as an empty tuple.
Also, the non-empty {} syntax can't be mistaken for a function 
literal because non-empty function literals always have at least 
one statement that ends with a semicolon.

2) Tuples and AliasSeq-s.
Correct me if I'm wrong, but I think that in all cases the 
expression is unambiguous:
auto t1 = {3, 4}; // Tuple
enum t2 = {3, 4}; // Tuple
alias t3 = {3, 4}; // AliasSeq

void foo(Tuple!(int, int) x);
foo({3, 4}); // same as foo(t1), or foo(t2)

template staticMap(alias F, T...);
alias constTypes = staticMap!(  constOf, { int, float, void[] }   
);
static assert (is(  constTypes == { const(int), const(float), 
const(void[]) }  );

writefln!({int, string, double})("%s %s %s", {3, "test", 4.2});


More information about the Digitalmars-d mailing list