unless guards in case statements
This commit is contained in:
@@ -214,11 +214,17 @@ class Helpers {
|
||||
function makeSwitchPattern(patternExp:ReaderExp):Array<Expr> {
|
||||
return switch (patternExp.def) {
|
||||
case CallExp({pos: _, def: Symbol("when")}, whenExps):
|
||||
patternExp.checkNumArgs(2, 2, "(when [guard] [pattern])");
|
||||
patternExp.checkNumArgs(2, 2, "(when <guard> <pattern>)");
|
||||
if (guard != null)
|
||||
throw CompileError.fromExp(caseExp, "case pattern can only have one `when` guard");
|
||||
throw CompileError.fromExp(caseExp, "case pattern can only have one `when` or `unless` guard");
|
||||
guard = macro Prelude.truthy(${k.convert(whenExps[0])});
|
||||
makeSwitchPattern(whenExps[1]);
|
||||
case CallExp({pos: _, def: Symbol("unless")}, whenExps):
|
||||
patternExp.checkNumArgs(2, 2, "(unless <guard> <pattern>)");
|
||||
if (guard != null)
|
||||
throw CompileError.fromExp(caseExp, "case pattern can only have one `when` or `unless` guard");
|
||||
guard = macro !Prelude.truthy(${k.convert(whenExps[0])});
|
||||
makeSwitchPattern(whenExps[1]);
|
||||
case ListEatingExp(exps) if (exps.length == 0):
|
||||
throw CompileError.fromExp(patternExp, "list-eating pattern should not be empty");
|
||||
case ListEatingExp(exps):
|
||||
|
@@ -369,6 +369,10 @@
|
||||
((when false (or 5 6)) (Assert.fail))
|
||||
((when true (or 7 8 9)) (Assert.fail))
|
||||
(otherwise (Assert.pass)))
|
||||
(case 5
|
||||
((unless true (or 5 6)) (Assert.fail))
|
||||
((unless false (or 7 8 9)) (Assert.fail))
|
||||
(otherwise (Assert.pass)))
|
||||
// In Haxe,
|
||||
// `switch (Some(true)) { case Some(true | false): "a"; default: "b"; }`
|
||||
// returns "a", so nested use of `or` in case patterns should also be valid:
|
||||
|
Reference in New Issue
Block a user