digitalmars.D - shorthand template and static conditionals?
- Jeremie Pelletier (3/3) Jul 29 2009 Could it be a valid option to have a shorthand template syntax if the te...
- Daniel Keep (13/17) Jul 29 2009 Aside from that not being how alias works,
- Lars T. Kyllingstad (10/11) Jul 30 2009 ?: already works at compile time, at least in D2. I use it all the time.
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
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
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