Pattern matching example

Lutger lutger.blijdestijn at gmail.com
Sun Apr 4 08:25:51 PDT 2010


Lutger wrote:

...
> 
> void main(string[] args)
> {
>     int i = 12;
>     matcher(i,
>           [ "aha": (string v) { writeln("I saw a string ", v); } ],
>           [ 12   : (int i) { writeln("I saw an int ", i); } ] );
> 
>     string v = "aha";
>     matcher(v,
>           [ "aha" : (string v) { writeln("I saw a string ", v); } ],
>           [ 12    : (int i) { writeln("I saw an int ", i); } ] );
> }

Forgot to add: it is of course possible to adjust matcher to accept something 
like this:


matcher(v,
     [ "aha" : (string v) { writeln("I saw a string ", v); },
       (string v) { writeln("default case for string ", v); },	
     [ 12    : (int i) { writeln("I saw an int ", i); } ] );



More information about the Digitalmars-d mailing list