Union types and exception handling

Kłeczek, Michał michal at kleczek.org
Thu May 14 07:56:35 UTC 2020


Hello All,

I am playing with javac lately looking for a way to implement some form 
of union types - along the lines of:

interface Function<S, T, E extends Exception> {
   T apply(S arg) throws E;
}

interface Stream<T, E extends Exception> {
   // resulting stream is parametrized with a union type
   <S, MapperEx extends Exception, ResultEx extends E | MapperEx> 
Stream<S, ResultEx> map(Function<? super T, S, MapperEx> mapper);
   boolean isEmpty() throws E;
}

<S, T, E extends IOException | ClassNotFoundException> T foo(Function<S, 
T, E> f, S arg) throws ClassNotFoundException {
   try {
     return f.apply(arg);
   }
   catch (IOException e) {
   }
}

A very limited form of union types is already implemented for 
multi-catch clauses and I am wondering
if:
- it can be reused
- anything more general has been discussed/prototyped - is anyone aware 
of such efforts?

Thanks,
Michal



More information about the amber-dev mailing list