ClassicSwitch.hx - a macro which takes a switch statement and turns it into an if/elseif/else chain. This is useful if you want traditional ecmascript "switch" behaviour, not the kick-ass haxe pattern matching. Times this is useful: if one of your cases is a variable:
option1 = "Something1";
option2 = "Something2";
switch (someVar)
{
case option1: trace ("do something 1");
case option2: trace ("do something 2");
}
This doesn't work with pattern matching, because option1
, option2
are seen as capture variables. With this macro, you could do:
option1 = "Something1";
option2 = "Something2";
ClassicSwitch.from(switch (someVar)
{
case option1: trace ("do something 1");
case option2: trace ("do something 2");
});
... and things will behave as expected.
Some quick tests in the Main.hx below. Run with haxe -x Main.hx
, in Haxe 3RC or above.