I'm trying to initialize two variables with an enhanced switch statement:
int num = //something
boolean val1;
String val2;
val1, val2 = switch(num) {
case 0 -> (true, "zero!");
case 1 -> (true, "one!");
default -> (false, "unknown :/");
}
Is this possible?
Since you are already on Java-13, I would suggest refraining from using an additional library to represent a tuple and make use ofMap.entry
(introduced in Java-9) with the syntactic sugar of local variable type var
inferred.
var entry = switch (num) {
case 0 -> Map.entry(true, "zero!");
case 1 -> Map.entry(true, "one!");
default -> Map.entry(false, "unknown :/");
};
boolean val1 = entry.getKey();
String val2 = entry.getValue();