HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[haxe.org/manual] break #29

Open utterances-bot opened 5 years ago

utterances-bot commented 5 years ago

break - Haxe - The Cross-platform Toolkit

Haxe is an open source toolkit based on a modern, high level, strictly typed programming language.

https://haxe.org/manual/expression-break.html

sonygod commented 5 years ago

if the break keyword in switch cases is not supported in Haxe. how to write something else instead of break key word?

markknol commented 5 years ago

You can't break a switch because either the switch pattern matches, or it doesn't. You can only break in loops.

You could use or patterns:

var value = "yes";
switch value {
    case "yes" | "true": trace("valid!");
     case "no" | "false": trace("invalid!");
    case v: trace("unknown: "+ v);
}

If you don't want it to match, you could add another condition to the case using guards case {expr} if (cond):. Note, there is no : inbetween.

This traces "yes"

var x = 123;
var y = 77;
switch (x) {
      case 321 if (y == 77):
            trace("no!");
      case 123 if (y == 66):
            trace("no!");
      case 123 if (y == 77):
            trace("yes!");
      case _: 
            trace("fake break");
}