r/java 4d ago

JEP draft: Structured Concurrency

https://openjdk.org/jeps/8389757
102 Upvotes

18 comments sorted by

View all comments

8

u/javaprof 4d ago

So checked exceptions doesn't work properly here too?

This is very funny:

try (var scope = StructuredTaskScope.open()) {
   ...
} catch (ExecutionException e) {
   Throwable cause = e.getCause(); 
   switch (cause) {
       case IOException ioe -> .. // what op caused this one?
       default -> .. // no exaustivness? 
   }
}

9

u/vips7L 4d ago edited 4d ago

Of course not the type system isn't strong enough to be generic over exceptions for lambdas. Swift has a pretty cool implementation of generic lambdas for typed throws.

public func count<E>(
    where predicate: (Element) throws(E) -> Bool
) throws(E) -> Int {
    print("Code goes here")
    return 0
}

When your lambda doesn't throw it just turns into throws(Never)

-7

u/javaprof 4d ago

I just feel that Java need to stop shipping things that perfectly fine could be libraries and just fix this elephant in the room. Ok, Valhalla and then error handling. Structured concurrency somewhat could be just figure out by the community for now

-2

u/vips7L 4d ago

I whole heartedly agree, I extensively use checked exceptions everywhere and even without fixing the type system there are so many easy wins to make them more usable (imo, and brian will of course disagree). Checked exceptions just have so much boilerplate. Little things like Swift's try? and try! operators would be so beneficial.

1

u/SleepingTabby 3d ago

Can you list some of those ways?

2

u/vips7L 3d ago edited 3d ago

Edit: formatting is fucked on this. I’ll have to fix it once I’m at a computer.

Of course.

try! or !! to automatically uncheck a checked exception when you can’t handle it. Right now you need to write several lines for this:

Something s;
try {
     s = fn();
} catch (SomeException ex) {
     throw new IllegalStateExceprtion(ex);
}

Something s = try! fn();

try? to automatically coerce into null:

Something s;
try {
    s = fn();
} catch (SomeException _) {
    s = null;
}

Something s = try? fn();
// combine that with the null operator
Something s = try? fn() ?? default();