logoalt Hacker News

masklinnlast Tuesday at 5:30 PM1 replyview on HN

> What would Rust's "From" look like, for example?

Idiomatic Go type-erases error types into `error`, when there is even a known type in the first place.

Thus `From` is not a consideration, because the only `From` you need is

    impl<'a, E> From<E> for Box<dyn Error + 'a>
    where
        E: Error + 'a,
and that means you can just build that in and nothing else (and it's really already built-in by the implicit upcasting of values into interfaces).

Replies

9rxlast Thursday at 6:29 PM

> Go type-erases error types

That's clearly not true.

    type MyError struct{}
    func (MyError) Error() string { return "MyError" }

    func main() {
        var err error = MyError{}
        fmt.Printf("%T\n", err) // Output: main.MyError
    }
The idea doesn't even make any sense. How could you even begin to handle errors in a meaningful way if you were unable to discern what type the error is?