JEP 427 and objects meant to replace null
P Holder
pholder at gmail.com
Tue Apr 26 23:55:08 UTC 2022
I was just looking at the null handling parts of
JEP 427: Pattern Matching for switch (Third Preview)
and wondering how it would impact the way I code.
I personally go to great efforts to avoid null. If I need an "out of
band" signal for an object, I make a public static final special
value to use in the place of null, especially in initializers.
I was wondering if there is any logical way to handle these special
cases? (sorry for the pun)
so if I have
class Shape
{
public static final Shape UNDEFINED_SHAPE = new Shape();
...
}
could I have
static void testTriangle(Shape s) {
switch (s) {
case null, UNDEFINED_SHAPE ->
System.out.println("An undefined shape");
case Triangle t
when t.calculateArea() > 100 ->
System.out.println("Large triangle");
default ->
System.out.println("A shape, possibly a small triangle");
}
}
More information about the amber-dev
mailing list