News for hackers. Stuff that matters.
Re:A new approach to error handling by brabel on Tuesday July 30, 2024 @01:58PM (#41112184)
You're completely right. Here's the example function signature from the blog post:

    fn greet() (string) (error)
In Java, you can write exactly the same thing:

    String greet() throws Error
The "implicit error propagation also looks exactly the same.

    fn main() () (error) {
        s := greet() // <-- Implicit error propagation
        println(s)
    }
In Java:

    void main() throws Error {
        var s = greet();  // <-- Implicit error propagation
        System.out.println(s);
    }
The case with a question mark after the error is the only case not covered by Java.

The final case, with explicit error handling:

    fn main() {
        s := greet() // <-- failure result can not be propagated
        println(s)
    }
Also looks exactly the same in Java:

    void main() {
        var s = greet(); // <-- compilation error: must handle Error
    }
EDIT: ok, let's mention also RuntimeException! In Java, you don't need to declare those (and subtypes) in your signature (but you can) and even if you do, you don't need to handle them. That's because the original idea was that RuntimeException was to represent programmer mistakes that should never be caught. The Java community ended up using it exactly the opposite way because everyone complained about explicit error handling being too much of a chore, so making most Exceptions subtype RuntimeException became common so now you can't assume that you don't need to catch them (but the compiler won't force you to)... other JVM languages like Groovy and Kotlin just eliminated checked Exceptions completely for that reason. It's a bit amusing to me how "checked errors" in general has made a comeback in Go and Rust, and in many newer languages like Zig (and now this one) as well. I think that the way Rust does it seems to be the best "compromise". It's almost like Java's checked Exceptions, but it's added sugar on top to make it much easier, while still explicit, to propagate errors. It's a tiny difference , but it seems that tiny difference makes ALL the difference for people (just see how people love Rust and its error handling, but loathe Java's).

Oracle guideline: https://docs.oracle.com/javase/tutorial/essential/exceptions...

Re:A new approach to error handling by Yoric on Tuesday July 30, 2024 @04:10PM (#41113622)
> It's a bit amusing to me how "checked errors" in general has made a comeback in Go and Rust, and in many newer languages like Zig (and now this one) as well.

For what it's worth, checked errors have been part of the ML family of languages (some of the immediate ancestors of Rust) since pretty much forever and of LISP even longer.

Still, let's give credit where it's due: very few people used them before Java.

Re:A new approach to error handling by marcosdumay on Tuesday July 30, 2024 @02:18PM (#41112399)
> It's a bit amusing to me how "checked errors" in general has made a comeback in Go and Rust

Go is... well, Go.

On other languages, it came back because they have polymorphism on the error type. Java is a huge mess because it has no implicit types nor most instances of polymorphism.

Re:A new approach to error handling by kaba0 on Tuesday July 30, 2024 @03:23PM (#41113173)
> Java is a huge mess because it has no implicit types nor most instances of polymorphism

Heh? What instance of polymorphism does it lack? Also, depending on what you mean by implicit types, Java has type inference nowadays.

Re:A new approach to error handling by dllthomas on Tuesday July 30, 2024 @03:27PM (#41113211)
IIUC, for checked exceptions Java lacks parametric polymorphism (generics).
Re:A new approach to error handling by kaba0 on Tuesday July 30, 2024 @03:32PM (#41113258)
Ah. But that’s also not true, you can do something like `throws T`.
Re:A new approach to error handling by Nullabillity on Tuesday July 30, 2024 @04:18PM (#41113702)
It works, until it doesn't. Exceptions are the one place in Java that supports union types, if you have code that can throw both Exception1 and Exception2 then Java can keep track of whether they are thrown separately:

    class Exception1 extends Exception {}
    class Exception2 extends Exception {}

    interface ThrowingFunction {
      void apply() throws Exception1, Exception2;
    }

    class Foo {
      public void doFoo(ThrowingFunction f) throws T {
        f.apply();
      }

      public static void main() throws Exception1, Exception2 {
        new Foo().doFoo(() -> {
          if (false) {
            throw new Exception1();
          }
          if (false) {
            throw new Exception2();
          }
        });
      }
    }}
However, if you pass them through a generic then they will be flattened into the closest common ancestor:

    class Exception1 extends Exception {}
    class Exception2 extends Exception {}

    interface ThrowingFunction<T extends Throwable> {
      void apply() throws T;
    }

    class Foo {
      public <T extends Throwable> void doFoo(ThrowingFunction<T> f) throws T {
        f.apply();
      }

      // Fails to compile, because Exception is still unhandled
      public static void main() throws Exception1, Exception2 {
        new Foo().doFoo(() -> {
          if (false) {
            throw new Exception1();
          }
          if (false) {
            throw new Exception2();
          }
        });
      }
    }
Re:A new approach to error handling by Yoric on Tuesday July 30, 2024 @04:11PM (#41113635)
I assume that the poster meant that Java could have had `Result<T, E>` return values like Rust if it had launched with parametric polymorphism, instead of having a layer of the type system dedicated to errors.
Re:A new approach to error handling by kaba0 on Tuesday July 30, 2024 @04:18PM (#41113705)
It still can have it, see e.g. Vavr, or other functional libraries.

Edit: Not sure why the downvote.

This works in Java today with proper pattern matching (I left out the complete name from the permit clause, it won’t compile as is):

  sealed interface <T,E> Result permits Ok, Error {
    record Ok(T val) implements Result<T,E> {}
    record Error(E err) implements Result<T,E> {}
  }
Re:A new approach to error handling by mrkeen on Tuesday July 30, 2024 @05:56PM (#41114518)
Man I hope Vavr survives. It is an oasis of rationality and I'm disheartened that https://docs.vavr.io/ appears to be down. Its Set implementation actually has union and intersection (!!!)
Re:A new approach to error handling by Yoric on Wednesday July 31, 2024 @03:54AM (#41117180)
Sure, but by now, the ship has long sailed, it's unlikely that many people will start adopting this style.
Re:A new approach to error handling by dllthomas on Tuesday July 30, 2024 @06:14PM (#41114646)
Ah, neat! I think the last time I looked you couldn't, but it was ages ago; I'm not surprised to have misremembered and even less surprised if it's changed.

Is it possible to provide a map method that re-throws whatever is thrown by its argument, without widening?

Re:A new approach to error handling by mrkeen on Tuesday July 30, 2024 @05:51PM (#41114488)
> What instance of polymorphism does it lack?

1) Higher-kinded. List<Integer> is OK and List<T> is OK, but T<Integer> is not.

2) Dispatch to the right method based on param types. If your class exposes foo(Apple) and foo(Banana), it will not route a Fruit to either of those methods even if it is one of those specific types.

3) Return type polymorphism, which is probably the most relevant to the error-handling discussion, e.g. in this example:

  fn greet() {
    fail "not greeting today"
  }
in another language, fail could be polymorphic over its return type - that is, if called from a function returning an Optional<>, fail itself could choose to return Optional.empty(). Or it could return the failed version of a CompletableFuture<>, or even throw a runtime exception depending on how the language was implemented.
Re:A new approach to error handling by lucianbr on Tuesday July 30, 2024 @02:16PM (#41112376)
Your analysis is spot-on.