static switch/pattern matching

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 25 02:12:12 PDT 2016


On Saturday, 25 June 2016 at 09:07:19 UTC, Lodovico Giaretta 
wrote:
>
> Instead of passing functions to match!, pass pairs of 
> arguments, like this:
>
>     match!(T,
>         int, writeln("Matched int"),
>         is(T : SomeObject), writeln("Derives from SomeObject");
>     );
>
> Now, in the implementation, foreach pair of arguments, if the 
> first member is a type that matches your target, perform that 
> branch; otherwise, if the first member is a boolean value, and 
> it is true, perform the branch.

Of course I meant:

      match!(T,
          int, () {writeln("Matched int");},
          is(T : SomeObject), () {writeln("Derives from 
SomeObject");}
      );

You could probably even match on the actual value (instead of its 
type) and pass it (correctly casted) to the functions:

      match!(t,
          int, (int t) {writeln("Matched int ", t);},
          is(T : SomeObject), (SomeObject t) {writeln(t, " derives 
from SomeObject");}
      );

I don't have time to implement it now, but I think it's not too 
difficult.


More information about the Digitalmars-d-learn mailing list