Money for Nothing, ...

Remi Forax forax at univ-mlv.fr
Tue Jun 13 19:17:25 UTC 2023


Hello,
currently, it's not possible to write a lot of generics sealed type because Java has no way to denote the bottom type.

By example, if a Result can be either a Success or an Error, we want to be able to write this kind of switch

  public static void main(String[] args) {
    Result<String, IOException> result = ...
    var val = switch(result) {
      case Error<IOException> error -> 1;
      case Success<String> success -> 2;
    };
  }

But i do not see a way to do that without introducing a way to denote the bottom type (named "Nothing" here)

  sealed interface Result<T,E extends Exception> {}
  record Error<E extends Exception>(E error) implements Result<Nothing, E> {}
  record Success<T>(T value) implements Result<T, Nothing> { }

Nothing being the return type of a method that either never terminate (by example, using a for(;;)) or always throw an exception.

So, should we add Nothing to Java or is there another way to model this kind of sealed types ?

regards,
Rémi


More information about the amber-spec-experts mailing list