5 ms·
You can do the same in Java now with sealed types: sealed interface Point {} record Cartesian(double x, double y) implements Point {} record Polar(
by azth 5y ago
You can do the same in Java now with sealed types:
sealed interface Point {}
record Cartesian(double x, double y) implements Point {}
record Polar(double r, double theta) implements Point {}
- gavinray 5y agoWith JDK 18 you even get binding patterns in matches, so you can extract + assign "Point.x"/"Point.y" to variables when matching on them!
- Twisol 5y agoType patterns [1] are still unfortunately in preview (and have been since JDK 17), and record (destructuring) patterns [2] haven't even landed as a preview yet. I hope the latter get at least a preview in JDK 19 [3], but it looks like they've barely even begun the process of targeting JEPs to 19 yet. [1]: https://openjdk.java.net/jeps/420 https://openjdk.java.net/jeps/420 [2]: https://openjdk.java.net/jeps/405 https://openjdk.java.net/jeps/405 [3]: https://openjdk.java.net/projects/jdk/19/ https://openjdk.java.net/projects/jdk/19/
- garethrowlands 5y agoGood point, that's an improvement.