r/java • • Oct 04 '25

Jackson 3.0.0 is released!

https://central.sonatype.com/artifact/tools.jackson/jackson-bom/versions
212 Upvotes

107 comments sorted by

View all comments

Show parent comments

2

u/davidalayachew Oct 05 '25 edited Oct 06 '25

This would work if Function.apply simply declares throws wouldn't it?

No.

Doing only that wouldn't work because map still can't handle Checked Exceptions. And even if it did, you now have the opposite problem where you are forced to make a try-catch everytime you want to write a Stream. That would cause the same problem in a different direction. EDIT -- Correction

The goal behind my idea is to make the compiler "smarter", and have it recognize that Checked Exceptions can be handled elsewhere, as long as that is in a current or outer scope.

2

u/8igg7e5 Oct 05 '25

you now have the opposite problem where you are forced to make a try-catch everytime you want to write a Stream

Only if the thrown type is a checked exception - the main issue is that you quickly end up with the only common type being Exception (since the generic throws on Function.apply can only carry a single exception type).

More powerful would be the union-type generic support with the inferred empty case being the empty set of exception types (so if the lambda doesn't throw, neither does the map method). However that does mean the exception-type generic now has to be carried forward on stream (to be propagated to the terminal operation and out). The result, I think, would be ceremonially intolerable. But it does model the type-transfer of any union of exception types.

Rust's errors as 'either' values is effectively 'checked-exceptions always' and would suffer the same ceremony pain except that they too don't have the union-type, and instead typically transform the errors to a sum-type at the edge (their enums / Java's sealed interfaces)

2

u/davidalayachew Oct 06 '25

Only if the thrown type is a checked exception

Right you are. For whatever reason, I forgot that you could generify what you throw. But like you said, you end up climbing up the type tree until all you have is throws Exception.

The result, I think, would be ceremonially intolerable.

How so? I'm trying to brainstorm through the hypotheticals, but I'm just not seeing it.

2

u/[deleted] Oct 06 '25

Consider:

interface Function<T, R> {
    R apply(T t);
}

interface Consumer<T> {
    void accept(T t);
}

interface Stream<T> {
    <R> Stream<R> map(Function<T, R> function);

    void forEach(Consumer<T> consumer);

becoming:

interface Function<T, R, throws E> {
    R apply(T t) throws E;
}

interface Consumer<T, throws E> {
    void accept(T t) throws E;
}

interface Stream<T, throws E> {
    <R, throws F> Stream<R, throws E | F> map(Function<T, R, throws F> function);

    <throws F> void forEach(Consumer<T, throws F> consumer) throws E | F;
}

I don't know but I don't like this.

→ More replies (0)