Static ternary if

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 24 22:00:23 PDT 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



More information about the Digitalmars-d-learn mailing list