The switch that only I like

by: Ethan McCue

One thing that I think is really cool, but no one else cares about, is that if you make a sealed interface which only has one implementor (say, if you are enriching a class via generated code) then there is actually a way for you to make a "type safe cast" between the two things.

What i mean by this is, consider a normal cast

Integer i = 3;
Object o = i;
String s = (String) o; // boom, ClassCastException

Normal casts are allowed by the language to fail. If you did not properly track the types of things and you try to cast to a type you are not compatible with your code will still compile, but it will crash at runtime.

Even if you write an interface that has a single implementor, the language can't guarantee that later on there won't be another one added.

interface Thing {
    void exist();
}

class OnlyThingImpl implements Thing {
    int specialThing = 8;

    @Override
    public void exist() {}
}

...

// This code works perfectly well
Thing thing = ...;
OnlyThingImpl thingImpl = (OnlyThingImpl) thing;
System.out.println(thingImpl.specialThing);

...

// but if an intern adds another implementation, java won't warn you about
// the old usage and it will crash at runtime if you happen to call that code
// with the wrong implementation.
class OtherThingImpl implements Thing {
    @Override
    public void exist() {
        System.out.println("party");
    }
}

Even making the interface sealed doesn't solve this, since Java won't force you to handle other possibilities in your casts if another case is added to the sealed class

sealed interface Thing permits OnlyThingImpl {
    void exist();
}

...

// This still has the same problem - you might forget about this code
OnlyThingImpl thingImpl = (OnlyThingImpl) thing;

What can solve this is a "safe cast" done via a pattern matching switch.

OnlyThingImpl thingImpl = switch (thing) {
    case OnlyThingImpl __ -> __;
};

<- Index