Last active
August 26, 2021 18:39
-
-
Save JanHolger/077a01634f0536fb135543cda7a6f6eb to your computer and use it in GitHub Desktop.
Abusing type pattern matching for integer ranges in switch (Java 17)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String [] args) { | |
Integer age = Integer.parseInt(args[0]); | |
switch(age) { | |
case Integer x && x < 0: | |
System.out.println("Not born yet!"); | |
break; | |
case Integer x && x < 18: | |
System.out.println("Minor!"); | |
break; | |
default: | |
System.out.println("Let's have a drink!"); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment