www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - shorthand template and static conditionals?

reply Jeremie Pelletier <jeremiep gmail.com> writes:
Could it be a valid option to have a shorthand template syntax if the template
body has only one statement? Something like:

template Tuple(A...) alias Tuple = A;

Another suggestion would be to have the ?: syntax supported for static
statements, as we're currently forced to use static ifs. It really adds a lot
of code to templates for even simple conditions.
Jul 29 2009
next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Jeremie Pelletier wrote:
 Could it be a valid option to have a shorthand template syntax if the template
body has only one statement? Something like:
 
 template Tuple(A...) alias Tuple = A;

Aside from that not being how alias works, template Tuple(A...) alias A Tuple; template Tuple(A...) { alias A Tuple; } You're saving two characters; hardly seems worth it, to me. That said, I can't think of any technical reason why you couldn't change the grammar to allow for a single declaration.
 Another suggestion would be to have the ?: syntax supported for static
statements, as we're currently forced to use static ifs. It really adds a lot
of code to templates for even simple conditions.

I don't think using ?: for statements is a good idea; it's only ever suitable for very small expressions. The problem there is that there would be no way to distinguish between a compile-time ?: and a run-time ?: for either the compiler or the programmer. I have enough grief with CTFE and kinda-static foreach, I don't want to see another construct like that added.
Jul 29 2009
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Jeremie Pelletier wrote:
 Another suggestion would be to have the ?: syntax supported for static
statements, as we're currently forced to use static ifs. It really adds a lot
of code to templates for even simple conditions.

?: already works at compile time, at least in D2. I use it all the time. template cat(string a, string b, bool addSpace) { enum string cat = a ~ (addSpace ? " " : "") ~ b; } static assert (cat!("foo", "bar", false) == "foobar"); static assert (cat!("foo", "bar", true) == "foo bar"); Is that what you meant? -Lars
Jul 30 2009