Result<T, E> in TypeScript: A Better Alternative to Throwing

https://slicker.me/typescript/error_handling.html

6 points · 4 comments · view on lemmy.world

4 Comments

artwork@lemmy.world · 2 pts · 129d (3 replies)

This is quite interesting, since the article states absolutely nothing about Go language.
Since, it's the most common or even fairly standard technique in the language:

func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return "", errors.New("empty name")
    }
    // ...
}

Source: go.dev/doc/tutorial/handle-errors

lbfgs@programming.dev · 5 pts · 128d

Go's val, err := maybeDoThing() pattern isn't the same thing because it's not a discriminated union like Rust's Result or C++'s std::expected or the Typescript example in the article. Moreover, due to restrictions in Go's generics, it's impossible to implement what is imo the most attractive pattern enabled by the discriminated union Result types - monadic operations.

felsiq@piefed.zip · 3 pts · 129d (1 reply)

Ditto for rust, although I didn’t read the article to know if they mentioned that one lol

artwork@lemmy.world · 2 pts · 129d

Thank you! Apparently, they did not mention Rust, too ^^"