2

I like to use switch/case a lot but I've been wondering if it's possible to mix a default with a case in Haxe (version 2 or 3)?

This is an example of what I'm trying to achieve, except it doesn't compile:

switch(s) {
    case 'reset': trace("...");
    case 'stat','stats': trace("...");        // This compiles ok in case some people don't know
    case 'help', default: trace("...");       // This doesn't compile
}

Do you know if this possible? I sometimes would prefer it this way because even though it's equivalent to just using a default, the code is sometimes more precise to the reader this way (no ambiguity or confusion).

2
  • 1
    Put the common code in a function, and then call the function from case 'help' and default:.
    – Barmar
    Aug 26, 2014 at 21:49
  • According to haxe.org/manual/lf-pattern-matching-or.html, in Haxe 3 it's also possible to write case 'stat' | 'stats': instead of case 'stat', 'stats' Aug 26, 2014 at 22:20

2 Answers 2

5

I just managed to figure out a working solution for Haxe 3. Unfortunately it doesn't compile in Haxe 2.10, the version that I'm using right now. (Edit: I found a way for Haxe 2, see the other answer below).

The documentation here (http://haxe.org/manual/lf-pattern-matching-introduction.html) says that that " a _ pattern matches anything, so case _: is equal to default: ".

So similarly to writing case 'stat', 'stats': I tried to write case 'help', _: and it compiles.

So a short for Haxe 3 is:

switch(s) {
    case 'reset': trace("...");
    case 'stat','stats': trace("...");
    case 'help', _: trace("...");
}

I compiled an example here : http://try.haxe.org/#A8D15

4
  • Also possible to write case 'help' | _: trace("..."); in Haxe 3. Aug 26, 2014 at 22:25
  • Big project started in 2011 and no real reason to port everything to Haxe 3 for now, since it works well. I think a lot of people are still using Haxe 2.10. Even though some new notations and simpler macros look exciting in Haxe 3 I see no imperious reasons to switch just yet. Would probably take a week or so to port, compile on servers, test etc. Aug 26, 2014 at 22:28
  • I guess the reason they added this feature in Haxe 3 is because there was no easy way to do it in Haxe 2. So your question there is likely to be unfulfilled.
    – Barmar
    Aug 27, 2014 at 16:06
  • No actually I found a way for Haxe 2, see my answer below. Oct 6, 2014 at 15:10
1

Here is an easy way that will work with Haxe 2.xx:

switch(s) {
    case 'reset': trace("...");
    case 'stat','stats': trace("...");
    case 'help', s /*!default!*/: trace("...");
}

So for Haxe 2.xx, all we have to do is repeat the expression inside the switch inside the final case and it will match anything. I recommend adding a comment just after for readability.

Strangely enough, this notation won't compile in Haxe 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.