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

Marc jckj33 at gmail.com
Tue Dec 19 21:19:41 UTC 2017


On Tuesday, 19 December 2017 at 15:52:57 UTC, Dgame wrote:
> On Tuesday, 19 December 2017 at 15:19:53 UTC, Marc wrote:
>> [...]
>
> template FirstOf(T...) {
>     template otherwise(D) {
>         static if (T.length == 0) {
>             enum otherwise = D.init;
>         } else {
>             enum otherwise = T[0];
>         }
>     }
>
>     template otherwise(alias value) {
>         static if (T.length == 0) {
>             enum otherwise = value;
>         } 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);
>     static assert (FirstOf!(1.5, "hello").otherwise!23 == 1.5);
>     static assert (FirstOf!().otherwise!42 == 42);
> }

Didn't know about alias as template paramter. Exactly what I 
wanted. Thank you!


More information about the Digitalmars-d-learn mailing list