COMMUNITY

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? https://haxe.org/manual/lf-pattern-matching-or.html

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
@:break
this use marco? I am not very familiar with macro.

@Gama11

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