Allow sealed interfaces without subclasses

Remi Forax forax at univ-mlv.fr
Thu Nov 26 07:48:55 UTC 2020


----- Mail original -----
> De: "Jakob Brünker" <jakob.bruenker at gmail.com>
> À: "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Jeudi 26 Novembre 2020 01:46:10
> Objet: Allow sealed interfaces without subclasses

> Hi all,
> 
> While I like sealed classes a lot in general, it's lacking one simple
> feature that prevents it from being used to fully emulate algebraic data
> types: You cannot make an uninhabited  type (modulo null), since you cannot
> have a sealed interface without subclasses.

A final class with a private constructor is what you are looking for.
A sealed type can be seen as an extension of the final concept if you want to list all subtypes instead of disallowing them.

And once Valhalla will be integrated, a primitive object type with no field and a private constructor is a unit type that even disallows null.

> 
> Is there a good reason to disallow this? It does come in handy every once
> in a while in languages where it's possible, especially when trying to
> convince the type checker that a particular combination of patterns is
> impossible. As a data point, in Haskell, this used to be impossible, but
> was later adopted into the language standard, and in fact is used in the
> standard library to define such a type under the name Data.Void.

Like we have java.lang.Void

> 
> Thanks,
> Jakob

regards,
Rémi


More information about the amber-dev mailing list