News for hackers. Stuff that matters.
Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @04:50PM (#23543919)
Because multiple return values for handling errors is a strictly inferior and error prone way for dealing with the matter.
Re:The Next Step for Generics by lenish on Tuesday June 16, 2020 @06:37PM (#23545156)

    func foo() (*SomeType, error) {
        ...
        return someErr
    }

    ...
    result, err := foo()
    if err != nil {
        // handle err
    }
    // handle result
vs

    type Result struct {
        Err error
        Data SomeType
    }

    func (r *Result) HasError() bool {
        return r.Err != nil
    }

    func bar() *Result {
        ...
        return &Result { ... }
    }

    ...
    result := bar()
    if result.HasError() {
       // handle result.Err
    }
    // handle result

I'm not really sure I see the benefit to the latter. In a language with special operators and built-in types it may be easier (e.g. foo()?.bar()?.commit()), but without these language features I don't see how the Result<T> approach is better.
Re:The Next Step for Generics by et1337 on Tuesday June 16, 2020 @06:56PM (#23545342)
Go can't really express the Result<T> approach. In Go, it's up to you to remember to check result.HasError(), just like it's up to you to check if err != nil. If you forget that check, you'll try to access the Data and get a nil pointer exception.

The Result<T> approach prevents you from accessing Data if you haven't handled the error, and it does so with a compile-time error.

Even with Go's draconian unused variable rules, I and my colleagues have been burned more than once by forgotten error checks.

Re:The Next Step for Generics by jatone on Tuesday June 16, 2020 @08:49PM (#23546338)
there are linters that will help you with that.

https://github.com/kisielk/errcheck

https://golangci-lint.run/usage/linters/ has a solid set of options.

Re:The Next Step for Generics by nimish on Wednesday June 17, 2020 @12:47AM (#23547906)
I just wish the linter was integrated into the compiler. And that code that didn't check would simply not compile
Re:The Next Step for Generics by mrkeen on Wednesday June 17, 2020 @11:55AM (#23552873)
> without these language features I don't see how the Result<T> approach is better.

That's the point! I want language features!

I don't want to wait 6 years for the designers to bake some new operator into the language. I want rich enough expression so that if '?.' is missing I just throw it in as a one-liner.

Generics is one such source of richness.

Re:The Next Step for Generics by whateveracct on Wednesday June 17, 2020 @03:47AM (#23548901)
A language with sun types will express Result as Success XOR Failure. And then to access the Success, the compiler will force you to go through a switch statement that handles each case.
Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @11:55PM (#23547586)
The alternative is not the Result type you defined, but something along the lines of what languages like Rust or Haskell define: https://doc.rust-lang.org/std/result/
Re:The Next Step for Generics by californical on Tuesday June 16, 2020 @05:03PM (#23544084)
It's interesting that you say this, because I've had the opposite experience. I wouldn't say it's strictly inferior, because there are definitely upsides. If it was strictly inferior, why would a modern language be designed that way -- there must be some debate right?

I love multiple returns/errors. I find that I never mistakenly forget to handle an error when the program won't compile because I forgot about the second return value.

I don't use go at work though, I use a language with lots of throw'ing exceptions, and I regularly miss handling exceptions that are hidden in dependencies. This isn't the end of the world in our case, but I prefer to be more explicit.

Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @05:06PM (#23544120)
> If it was strictly inferior, why would a modern language be designed that way

golang is not a modern language (how old it is is irrelevent), and the people who designed it did not have a proper language design background (their other accomplishments are a different matter).

Having worked on larger golang code bases, and I've seen several times where errors are either ignored or overwritten accidentally. It's just bad language design.

Re:The Next Step for Generics by dgb23 on Tuesday June 16, 2020 @06:47PM (#23545255)
I cannot think of a language where errors cannot be ignored. In go it is easy to ignore them, but they stick out and can be marked by static analysis. The problems you describe are not solved at the language level, but by giving programmers enough time and incentives to write durable code.
Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @08:55PM (#23546402)
The following line in golang ignores the error:

    fmt.Println("foo")
Compare to a language with exception handling where an exception will get thrown and bubbles up the stack until it either hits a handler, or crashes the program with a stack trace.

And I was referring to accidental ignoring. I've seen variations of the following several times now:

    res, err := foo("foo")
    if err != nil { ... }
    if res != nil { ... }
    res, err = foo("bar")
    if res != nil { ... }
Re:The Next Step for Generics by Mawr on Tuesday June 16, 2020 @11:07PM (#23547298)
Usage of linters fixes this:

>The following line in golang ignores the error:

   fmt.Println("foo")
fmt.Println() is blacklisted for obvious reasons, but this:

    a := func() error {
        return nil 
    }
    a()
results in:

    go-lint: Error return value of 'a' is not checked (errcheck)
>And I was referring to accidental ignoring. I've seen variations of the following several times now:

    res, err := foo("foo")
    if err != nil { ... }
    if res != nil { ... }
    res, err = foo("bar")
    if res != nil { ... }
results in:

    go-lint: ineffectual assignment to 'err' (ineffassign)
Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @11:50PM (#23547550)
> fmt.Println() is blacklisted for obvious reasons

That's the issue with the language, there are so many special cases for convenience sake, not for correctness sake. It's obvious why it's excluded, but it doesn't make it correct. Do you want critical software written in such a language?

Furthermore, does that linter work with something like gorm (https://gorm.io/) and its way of handling errors? It's extremely easy to mis-handle errors with it. It's even a widely used library.

Re:The Next Step for Generics by geodel on Tuesday June 16, 2020 @11:02PM (#23547261)
Huh, I have seen enough catch blocks in Java code at work which are totally empty. How is that better than ignoring error?
Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @11:51PM (#23547558)
Because it's an explicit opt-in, as opposed to accidental opt out. And static checking can warn you about empty catch blocks.
Re:The Next Step for Generics by umanwizard on Tuesday June 16, 2020 @11:11PM (#23547318)
In rust, errors are difficult to ignore (you need to either allow compiler warnings, which AFAICT nobody sane does, or write something like `let _ = my_fallible_function();` which makes the intent to ignore the error explicit).

Perhaps more fundamental: it’s impossible to accidentally use an uninitialized “success” return value when the function actually failed, which is easy to do in C, C++, Go, etc.

Re:The Next Step for Generics by ori_b on Wednesday June 17, 2020 @12:03AM (#23547639)
Or .unwrap(), which I see relatively often.
Re:The Next Step for Generics by umanwizard on Wednesday June 17, 2020 @08:43AM (#23550684)
That’s not ignoring errors, it’s explicitly choosing what to do in case of one (crash).
Re:The Next Step for Generics by fooster on Tuesday June 16, 2020 @06:06PM (#23544866)
Error handling is hard, period. Error handling in go is no worse than any other language, and in most ways it is better being explicit and non-magic.

> people who designed it did not have a proper language design background

Irrelevant.

> It's just bad language design.

try { ... } catch(Exception ex) { ... }

Re:The Next Step for Generics by entha_saava on Wednesday June 17, 2020 @12:06AM (#23547657)
Exceptions don't lead to silent but dangerous and hard to debug errors. The program fails if exception is not handled.
Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @09:03PM (#23546452)
> try { ... } catch(Exception ex) { ... }

The error here is explicitly handled, and cannot be accidentally ignored. Unlike golang where it's quite easy for errors to go ignored accidentally.

Re:The Next Step for Generics by fooster on Tuesday June 16, 2020 @09:42PM (#23546751)
Nevertheless, this is how it is mostly done in Java. I haven't used eclipse in eons, but last time I did it even generated this code.

If you care with go, use errcheck.

Re:The Next Step for Generics by apta on Wednesday June 17, 2020 @12:02AM (#23547631)
Does errcheck work well with gorm (https://gorm.io/) and it's way of returning errors? This is not an obscure library, it's quite widely used.
Re:The Next Step for Generics by ori_b on Wednesday June 17, 2020 @12:24AM (#23547771)
Does any language save you from explicitly screwing up error handling? Gorm is doing the Go equivalent of:

     class Query {
         class QueryResult {
             Exception error;
             Value result;
         }
         public QueryResult query() {
             try {
                 return doThing();
             } catch(Exception e){
                 return new QueryResult(error, null);
             }
         }
     }
Gorm is going out of its way to make error handling suck.
Re:The Next Step for Generics by apta on Wednesday June 17, 2020 @03:46PM (#23555856)
> Does any language save you from explicitly screwing up error handling?

It's about the default error handling method being sane. In exception based languages, an unhandled error bubbles up until it reaches a handler, or it crashes the program with a stacktrace.

Compare to what golang does, it's somewhat easy to accidentally ignore or overwrite errors. This leads to silent corruption of state, much worse than crashing the program outright.

Re:The Next Step for Generics by ori_b on Wednesday June 17, 2020 @05:14PM (#23556859)
> It's about the default error handling method being sane.

Gorm isn't using the default error handling.

Re:The Next Step for Generics by apta on Wednesday June 17, 2020 @07:21PM (#23558019)
That's one point in this discussion. The language allows error handling that way. Compared to a language with proper sum types or exceptions, where one would have to actively work against the language to end up with that mess.
Re:The Next Step for Generics by ori_b on Wednesday June 17, 2020 @08:26PM (#23558471)
> That's one point in this discussion. The language allows error handling that way. Compared to a language with proper sum types or exceptions, where one would have to actively work against the language to end up with that mess.

I've seen a bunch of code that does the equivalent of the Java I posted above. Mostly when sending errors across the network.

Re:The Next Step for Generics by hactually on Tuesday June 16, 2020 @09:11PM (#23546512)
because it has try/catch. Without that (which would be similar to not checking the err in go) it explodes or throws to a layer up that may not expect it.

Each language has its wonks.

Re:The Next Step for Generics by apta on Tuesday June 16, 2020 @11:53PM (#23547572)
> Without that (which would be similar to not checking the err in go) it explodes or throws to a layer up that may not expect it.

It's not similar to that at all. Without it, the exception bubbles up until it gets caught somewhere, or crashes the program with a useful stacktrace.

With golang, it just goes undetected, and the code keeps running with corrupt state, without anyone knowing any better.

Re:The Next Step for Generics by dgb23 on Tuesday June 16, 2020 @06:41PM (#23545193)
I would say it is a very ergonomic way of doing this. It allows for writing in a more exploratory way until you know what your error handling story is. Then, even if you choose to propagate it later, you just add it to your signature. Also it is very easy to grok and clear. Definitely not strictly inferior.