Language feature to improve checked exceptions

Holo The Sage Wolf holo3146 at gmail.com
Tue Mar 7 13:24:43 UTC 2023


This is a continuation of this thread
<https://mail.openjdk.org/pipermail/amber-dev/2023-March/007849.html>,
unfortunately I only joined the mailing list, so I can’t reply to it so I
apologize for opening a new thread.

I want to point to Koka
<https://koka-lang.github.io/koka/doc/book.html#why-effects>, Koka is a
language built upon the concept of Effect, a generalization of how Java
treats checked exceptions (and similarly to what Brian Goetz said about how
Checked exceptions are equivalent to Either, general Effect system is
equivalent to full monadic type system).

The language is very interesting and worth reading about, but the relevant
part is their Polymorphic Effects, which allow you to extend the effects.
In Java pseudo code, it means that the following is valid:

@FunctionalInterfacepublic interface EPredicate<T, E extends Throwable> {
    boolean test(T t) throws E;
}
@FunctionalInterfacepublic interface EConsumer<T, E extends Throwable> {
    boolean test(T t) throws E;
}
public class EStream<V,E extends Throwable> {
    public <EX extends Throwable> EStream<V, (E|EX)>
filter(EPredicate<? super T, (E|EX)> predicate) {
        ...
    }

    public <EX extends Throwable> void forEach(EConsumer<? super T,
(E|EX)> action) throws E, EX {
        ...
    }
}

Which allows you to have a fluent composition without losing the
fine-grained checked exceptions:

public static void main(string[] args) throws FileNotFoundException,
IllegalAccessException, SQLException {
    myEStream.filter(v -> ...) // something that throws FileNotFoundException
                     .filter(v -> ...) // something that throws
IllegalAccessException
                     .forEach(v -> ...); // something that throws SQLException
}

This also makes the type system of the throws clause a subset of the
generics type system.

While I do believe that the actual solution will require much more thought
than this (e.g. deconstruction of generic sums to be able to handle
specific exceptions and remove those exceptions from the final union type
of exceptions), this is a showcase that a nice and composable checked
exceptions works even without Monadic types.

I find this direction much nicer than using Either everywhere.
-- 
Holo The Wise Wolf Of Yoitsu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230307/b56e32fd/attachment-0001.htm>


More information about the amber-dev mailing list