cool pattern matching template

Phil via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 1 11:54:55 PST 2015


Could you give a concrete example of using your Match template?

e.g. to perform a different action for floating point or integral 
values.

> Hey all, I've found myself leaning on a really versatile pattern
> a lot lately, thought I'd share it: this is for those situations
> where you might have something like
>
>    static if (is (typeof(some stuff)))
>      return some stuff;
>    else static if (__traits(compiles, more things))
>      return more things;
>
> or perhaps some other repetitive or confusing piece of
> metaprogramming logic whose goal is to decide what code to
> execute.
>
> instead, try:
>
>    auto option_a ()() {return some stuff;}
>    auto option_b ()() {return more things;}
>
>    return Match!(option_a, option_b);


More information about the Digitalmars-d mailing list