Last active
October 8, 2025 09:41
-
-
Save fatso83/33edb31decdf9c9ce7be109ec9089834 to your computer and use it in GitHub Desktop.
Sealed classes switching on instanceof
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
| import java.util.List; | |
| /* | |
| * ❯ java ./SealedSwitch.java | |
| * SHIZZLES FOR MY NIZZLES!!! | |
| * Whooof! | |
| * Whooof! | |
| * Whooof! | |
| */ | |
| public class SealedSwitch { | |
| sealed interface Effect permits Shout, Bark { | |
| } | |
| record Shout(String value) implements Effect { } | |
| record Bark(int times) implements Effect {} | |
| public static void main(String ...args){ | |
| final var effects = List.of(new Shout("Shizzles for my nizzles"), new Bark(3)); | |
| for(var effect: effects) { | |
| switch(effect) { | |
| case Bark b -> { | |
| for(int i = 0; i < b.times; i++) | |
| System.out.println("Whooof!"); | |
| } | |
| case Shout s -> System.out.println(s.value.toUpperCase() + "!!!"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment