A possible static ternary operator syntax:

enum foo = ct_cond !? Foo!5 : Bar!6;

But in my opinion the need for it is not strong enough, better to keep the language simpler.

A static ternary operator is sometimes handy, this is working code:

import std.typetuple: TypeTuple;

template Iota(int n) {
    static if (n <= 0)
        alias TypeTuple!() Iota;
    else
        alias TypeTuple!(Iota!(n-1), n-1) Iota;
}

void main() {
    int[3] a, b;
    foreach (i; Iota!3)
        a[i] = b[i];
}


With the recently introduced syntax for enum and templates you could assume this works:

enum Iota(int n) = (n <= 0) ?
                   TypeTuple!() :
                   TypeTuple!(Iota!(n-1), n-1);

But that's a regular ternary operator, so despite only one branch is computed, both are verified for type, because they have to return the same type, so it gives:

Error: template instance test.Iota!-497 recursive expansion

A static ternary operator is allowed to return two different types, so no type is computed for the other branch, and this works:

enum Iota(int n) = (n <= 0) !?
                   TypeTuple!() :
                   TypeTuple!(Iota!(n-1), n-1);

Bye,
bearophile

Reply via email to