How do I pass a type as parameter in this method?

Ali Çehreli acehreli at yahoo.com
Tue Dec 19 00:01:00 UTC 2017


On 12/18/2017 03:54 PM, Ali Çehreli wrote:
> On 12/18/2017 02:58 PM, Marc wrote:

Here's another experiment:

template FirstOf(T...) {
     template otherwise(D) {
         static if (T.length == 0) {
             enum otherwise = D.init;
         } else {
             enum otherwise = T[0];
         }
     }
}

void main() {
     static assert (FirstOf!(1.5, "hello").otherwise!int == 1.5);
     static assert (FirstOf!("world", [1]).otherwise!int == "world");
     static assert (FirstOf!().otherwise!int == 0);
}

Ali


More information about the Digitalmars-d-learn mailing list