digitalmars.D.learn - Static ternary if
- Gorge Jingale (3/3) Jul 24 2016 Is there a static ternary if?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (10/13) Jul 24 2016 The way to force an expression at compile time is to use it for
- lqjglkqjsg (21/36) Jul 25 2016 It also works for "real" enumerated types.
- Cauterite (11/14) Jul 25 2016 You can pretty easily make your own;
- Gorge Jingale (4/20) Jul 25 2016 Cool, that would work. I don't think the lazy evaluation is a
- Michael Coulombe (3/26) Jul 25 2016 If that's ok, then try out std.traits.Select or std.traits.select:
- Cauterite (3/6) Jul 26 2016 Damn, I looked real hard for that template, I knew it existed. I
Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if.
Jul 24 2016
On 07/24/2016 07:15 PM, Gorge Jingale wrote:Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if.The way to force an expression at compile time is to use it for something that's needed at compile time. For example, you can initialize a manifest constant (enum) with that expression: void main() { enum i = (__MODULE__.length % 2) ? 42 : 43; pragma(msg, i); } Instead of enum, you can use 'static const' as well. Ali
Jul 24 2016
On Monday, 25 July 2016 at 05:00:23 UTC, Ali Çehreli wrote:On 07/24/2016 07:15 PM, Gorge Jingale wrote:It also works for "real" enumerated types. ******************************** enum ver = 0; // version(Windows) ... else .... enum VersionRelative1 { A = ver ? 1 : 2, B = ver ? 3 : 4, } enum VersionRelative2 { A = !ver ? 1 : 2, B = !ver ? 3 : 4, } unittest { static assert(VersionRelative1.A == 2); static assert(VersionRelative2.A == 1); } ******************************** which is quite cool and not widely known.Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if.The way to force an expression at compile time is to use it for something that's needed at compile time. For example, you can initialize a manifest constant (enum) with that expression: void main() { enum i = (__MODULE__.length % 2) ? 42 : 43; pragma(msg, i); } Instead of enum, you can use 'static const' as well. Ali
Jul 25 2016
On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote:Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if.You can pretty easily make your own; template staticIf(bool cond, alias a, alias b) { static if (cond) { alias staticIf = a; } else { alias staticIf = b; }; }; The drawback is that there's no 'short-circuiting'; a and b are both evaluated.
Jul 25 2016
On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote:On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote:Cool, that would work. I don't think the lazy evaluation is a problem at compile time? Just makes for longer times, but should be pretty minuscule.Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if.You can pretty easily make your own; template staticIf(bool cond, alias a, alias b) { static if (cond) { alias staticIf = a; } else { alias staticIf = b; }; }; The drawback is that there's no 'short-circuiting'; a and b are both evaluated.
Jul 25 2016
On Monday, 25 July 2016 at 22:57:05 UTC, Gorge Jingale wrote:On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote:If that's ok, then try out std.traits.Select or std.traits.select: https://dlang.org/phobos/std_traits.html#SelectOn Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote:Cool, that would work. I don't think the lazy evaluation is a problem at compile time? Just makes for longer times, but should be pretty minuscule.Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if.You can pretty easily make your own; template staticIf(bool cond, alias a, alias b) { static if (cond) { alias staticIf = a; } else { alias staticIf = b; }; }; The drawback is that there's no 'short-circuiting'; a and b are both evaluated.
Jul 25 2016
On Tuesday, 26 July 2016 at 00:54:59 UTC, Michael Coulombe wrote:If that's ok, then try out std.traits.Select or std.traits.select: https://dlang.org/phobos/std_traits.html#SelectDamn, I looked real hard for that template, I knew it existed. I expected it to be in std.meta though.
Jul 26 2016