On 2015-10-26 12:40, Edmund Smith wrote:

You could also emulate constant matching using default parameters
(albeit with the restriction that they must be after any
non-default/constant parameters), since the defaults form part of the
function's type. I tried making something like this earlier this summer
and it'd check that a given value was first equal to the default
parameter and match if so, or match if there was no default parameter
but the types matched.

e.g.
//template ma(tch/g)ic

unittest
{
     Algebraic!(string, int, double, MyStruct) v = 5;
     string s = v.match!(
         (string s = "") => "Empty string!",
         (string s) => s,
         (int i = 7) => "Lucky number 7",
         (int i = 0) => "Nil",
         (int i) => i.to!string,
         (double d) => d.to!string,
         (MyStruct m = MyStruct(15)) => "Special MyStruct value",
         (MyStruct m) => m.name, //
         () => "ooer");
     writeln(s);
}

It's a bit ugly overloading language features like this, but it makes
the syntax a little prettier.

Why not just use a value as an extra argument:

v.match!(
    7, (int i) => "Lucky number 7"
);

Scala's Option is really nice on the other hand since you can/should pattern 
match).

I thought it was preferred to use the higher order functions like "map" and "filter".

--
/Jacob Carlborg

Reply via email to