Pattern matching in D
Walter Bright
newshound1 at digitalmars.com
Sun Mar 7 15:38:07 PST 2010
retard wrote:
> Sun, 07 Mar 2010 11:17:46 -0800, Walter Bright wrote:
>
>> retard wrote:
>>> - Pattern matching (extension to enum/string/integer accepting switch)
>> Andrei and Sean have shown how to do that nicely with existing language
>> features.
>
> Really? I'd really like to see how this is done.
foo( v,
(int i) { writeln("I saw an int ", i); },
(string s) { writeln("I saw a string ", s); ),
(Variant any) { writeln("I saw the default case ", any); }
);
foo is a variadic template which takes its first argument, v, and
attempts to match it with each delegate in turn. The first one that
matches is executed.
The matching is all done at compile time, of course, and the delegate
can be inlined.
More information about the Digitalmars-d
mailing list