How to do the same thing like break switch?

how to do the same thing like break switch?

the haxe docs say: do not support break in a switch? how to do the same thing?

Are you looking for or-patterns? Or patterns - Haxe - The Cross-platform Toolkit

I have some code like this

  case CT_BOMB: 

  			if (bCardCount < bTurnCardCount)
  				break;//how to to this in Haxe?

Just invert the condition:

case CT_BOMB:
	if (bCardCount >= bTurnCardCount) {
		// code after the old if

If nothing happens after the switch you could also use return.

can you provided something like
this use marco? I am not very familiar with macro.


I mean, you probably could, but it really doesn’t seem worth the effort and complexity…

ok,if someone else can provide something like @:break will be a pleasure , current I use your suggestion.

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):
      case 123 if (y == 66):
      case 123 if (y == 77):
      case _: 
        	trace("fake break");
1 Like