Am 10.05.2013 17:11, schrieb clemos:
>
> So I wrote a small macro that allows both pattern matching and old
> style case statements with existing variable identifiers:
>
https://gist.github.com/clemos/5443026
> It has not been heavily tested, but should work as expected in most cases.
Try compiling this:
var x = macro 1;
MyMacro.HybridSwitch.build(switch(x.expr) {
case EParenthesis(x): x;
case _: null;
});
EParenthesis(x) is a valid ECall in the context, so your macro replaces
it with
case __value__ if(__value__==EParenthesis(x))
which doesn't fly with the compiler.
In general I think that "pattern can be typed" is not a strong enough
criteria, because patterns and especially identifiers can be typed for
all sorts of reason. You may be in luck if you handle above enum case
and everything works out somehow, but that macro will still turn a "case
true" into "case __value__ if (__value__ == true)", which is a bit awkward.
Simon