Hi List,
just stumbled upon a gotcha with pattern matching, which I wanted to share. We have a new coworker, who starts working with Haxe. He wrote some code, like the snippet below. In the case of the "h" he omitted the quotes and to my surprise the code did compile. It took me a while to grasp that pattern matching did its magic here:
class Test {
static function main() {
var input = "x";
var answer = "";
switch (input) {
case "l":
answer = "input was l";
case h:
answer = "input was h";
default:
answer = "dont know";
}
trace(answer); // "input was h"
}
}
this can lead to nasty errors. Imagine this:
class Test {
static inline var keyUP = "u";
static inline var keyDOWN = "d";
static function main() {
var input = "d";
var answer = "";
switch (input) {
case keyUPS:
answer = "input was up";
case keyDOWN:
answer = "input was down";
default:
answer = "dont know";
}
trace(answer); // "input was up"
}
}
now I know, that it is they way it is. Constants should be UPPERCASE !