HaxeFoundation / HaxeFoundation/haxe
Switch makes if in case an expression
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
The first `addEventListener` works, but in the second I wrap the if/elseif in a switch and I get the following error:
```
[ERROR] Test.hx:21: lines 21-23
21 | } else if (event.code == "ArrowRight") {
22 | rightKey = true;
23 | }
|
| Void should be Bool
```
The if now requires all its branch to unify to Bool despite not being used as an expression,
shown in the third `addEventListener` where I added an `else` with a Bool value and it works.
The fourth `addEventListener` has a return after the switch and the error doesn't trigger, but it seems that any statement works.
If instead of `addEventListener` I use a function where the argument is typed as `(event:js.html.KeyboardEvent)->Void` it works, but with `haxe.Constraints.Function` the error is present.
https://try.haxe.org/#5211741D
```haxe
class Test {
static function main() {
var leftKey = false;
var rightKey = true;
js.Browser.document.addEventListener("keydown", (event:js.html.KeyboardEvent) -> {
if (event.code == "ArrowLeft") {
leftKey = true;
} else if (event.code == "ArrowRight") {
rightKey = true;
}
});
js.Browser.document.addEventListener("keydown", (event:js.html.KeyboardEvent) -> {
switch (0) {
case _:
case 0:
if (event.code == "ArrowLeft") {
leftKey = true;
} else if (event.code == "ArrowRight") {
rightKey = true;
}
}
});
js.Browser.document.addEventListener("keydown", (event:js.html.KeyboardEvent) -> {
switch (0) {
case _:
case 0:
if (event.code == "ArrowLeft") {
leftKey = true;
} else if (event.code == "ArrowRight") {
rightKey = true;
} else {
true;
}
}
});
js.Browser.document.addEventListener("keydown", (event:js.html.KeyboardEvent) -> {
switch (0) {
case _:
case 0:
if (event.code == "ArrowLeft") {
leftKey = true;
} else if (event.code == "ArrowRight") {
rightKey = true;
}
}
return;
});
}
}
```
Contributor guide
Research direction
Start with the Test.hx reproduction at lines 21–23 and run the linked try.haxe example to observe the Void should be Bool error. Trace how switch branches and callback types involving haxe.Constraints.Function are unified; done when the reproduction compiles without the erroneous error and the behavior is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100