www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static ternary if

reply Gorge Jingale <Frifj mail.com> writes:
Is there a static ternary if?

(A == B) ? C : D;

for compile type that works like static if.
Jul 24 2016
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
parent lqjglkqjsg <lqjglkqghjghjjsg lkdsf.od> writes:
On Monday, 25 July 2016 at 05:00:23 UTC, Ali Çehreli wrote:
 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
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.
Jul 25 2016
prev sibling parent reply Cauterite <cauterite gmail.com> writes:
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
parent reply Gorge Jingale <Frifj mail.com> writes:
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:
 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.
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.
Jul 25 2016
parent reply Michael Coulombe <kirsybuu gmail.com> writes:
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:
 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.
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.
If that's ok, then try out std.traits.Select or std.traits.select: https://dlang.org/phobos/std_traits.html#Select
Jul 25 2016
parent Cauterite <cauterite gmail.com> writes:
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#Select
Damn, I looked real hard for that template, I knew it existed. I expected it to be in std.meta though.
Jul 26 2016