Small question regarding Java generics & sealed interfaces

Tesla Zhang ice1000kotlin at foxmail.com
Thu Feb 23 07:54:25 UTC 2023


Hi all, I am experimenting with Java generics and I have the following question (all the code in this email can be found at https://gist.github.com/ice1000/e376089438dd115c369329d00ee27779).

Here's some simple prelude definitions (the gist is more informative):


sealed interface Nat<T extends Nat<T>> {}
record Z() implements Nat<Z> {}
record S<T extends Nat<T>>(Nat<T> pred) implements Nat<S<T>> {}

sealed interface Fin<T extends Nat<T>> {}
record FZ<T extends Nat<T>>() implements Fin<S<T>> {}
record FS<T extends Nat<T>>(Fin<T> pred) implements Fin<S<T>> {}



From the definition, it is obvious that Fin<Z> is an empty type, but switching over an instance of that type still requires clauses (which, for whatever I write, there will be a type error unless I use raw generics). I would like to know the current design decisions here.


static void empty(Fin<Z> fin) {
  String s = switch (fin) {
  };
}




Also, it would be nice if switching `Fin<S<Z>>` can elide the case for `FS` because the only possible instance of such type is `new FS<>(null)`, but I understand why it doesn't work. It's a shame that we still have nulls to deal with :(


Best regards and cheers,
Tesla
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230223/62f4fc74/attachment.htm>


More information about the amber-dev mailing list