New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the StringSwitch and EnumSwitch constructs #143
Conversation
edfd96a
to
06a4196
Compare
06a4196
to
78deed4
Compare
fc14334
to
50e2116
Compare
I didn't review properly (yet), but I've got 2 questions about the API:
s.caseOf("boom");
s.caseOf("foo", bc -> {
bc.returnValue(bc.load("fooo"));
}); looks closer to the corresponding Java source code, but wouldn't s.caseOf(Set.of("boom", "foo"), bc -> {
bc.returnValue(bc.load("fooo"));
}); actually be a better API?
|
+1 But I wonder if we need to keep the ordering or not..? I mean the result should be the same but the evaluation order not.
I'm not so sure it's reasonable to "break" by default. I mean it's not "expected". |
I think the Fall through is a contentious topic, because Java has it and we should be close to Java, but my Strong Opinion here is that Java should have never copied this from C(++) and Go did the Right Thing (https://golangbot.com/switch/#fallthrough). I guess I don't mind too much though. |
Hm, you're right that the |
Java 17's new switch syntax works in this way FWIW. |
Hm, you mean the We could just provide two variants of the method. One for the "switch labeled statement group" ( We could even add the // equivalent of case L ->
StringSwitch s1 = method.stringSwitch(val);
s1.caseOf("foo", bc -> bc.assign(ret, bc.load("fooo")));
s1.caseOf("bar", bc -> bc.assign(ret, bc.load("barr")));
// equivalent of case L:
StringSwitch s2 = method.stringSwitch(val);
s.fallThrough();
s2.caseOf("foo", bc -> bc.assign(ret, bc.load("fooo")));
s2.caseOf("bar", bc -> bc.assign(ret, bc.load("barr"))); |
I've added the |
Sorry it took me a while, I had to browse through this in my IDE and look at the generated bytecode to be able to understand what's going on. LGTM. |
(I also originally wanted to say that perhaps the |
No problem.
Well, the |
FYI I need to send a follow-up PR to fix the problem when using result handles from the enclosing scope. |
This pull request introduces the
StringSwitch
andEnumSwitch
constructs that can be used to generate bytecode similar to the Java switch.API usage examples:
The
StringSwitch
is implemented with thelookupswitch
instruction while theEnumSwitch
is implemented with thetableswitch
instruction. In general, the resulting bytecode is very similar to the bytecode generated byjavac
.We could also introduce a switch for
int
but enums and strings are probably more common.