News for hackers. Stuff that matters.
Re:Go Replaces Interface{} with 'Any' by kbd on Tuesday December 14, 2021 @04:34PM (#29558192)
This is fantastic. It'll make Go feel much less weird. eg from the diff:

  []interface{}{1, 2.0, "hi"} -> []any{1, 2.0, "hi"}
Now that Go is going to have generics, all we need is sugar syntax for early return on error -- like more modern languages such as Rust and Zig have -- and Go may finally be pleasant to program in!
Re:Go Replaces Interface{} with 'Any' by hbn on Tuesday December 14, 2021 @05:06PM (#29558669)
It's definitely more streamlined, but my concern would be that newcomers might not understand that it's just an alias. Learning Go really reframed my concept of what an interface is, and I thought that using an empty interface to represent "any type" was kind of ingenious, and helps reinforce its ethos.

An empty interface can represent any type because every type inherently implements an interface with no methods. And that's what Go is all about -- implicitly implementing interfaces.

If a newbie hops into Go and just starts using "any" I think they might assume it's a magic type that's at the base of everything, missing out on the fact that they're still taking advantage of interfaces.

Re:Go Replaces Interface{} with 'Any' by benhoyt on Tuesday December 14, 2021 @07:16PM (#29560115)
Counterpoint: I'm an experienced Go dev but still think of "interface{}" as a semi-special "any" type, just because it fits my brain better conceptually. "Any type" is simpler than "an interface with an empty method set, which is of course matched by any type". I think this is a win.
Re:Go Replaces Interface{} with 'Any' by pawelmurias on Tuesday December 14, 2021 @06:55PM (#29559888)
A language shouldnt be optimized towards doc avoiding newbies at the cost of making things verbose and ugly.
Re:Go Replaces Interface{} with 'Any' by jshen on Tuesday December 14, 2021 @09:21PM (#29561266)
Go is the most readable language I’ve used in terms of understanding other peoples complex code bases, and I’ve been doing this a long time and have used a lot of languages.

Edit: It’s also one of the most approachable languages.

Re:Go Replaces Interface{} with 'Any' by valenterry on Tuesday December 14, 2021 @11:02PM (#29561944)
Why don't you list the languages that you have used? Otherwise there isn't really any new information.

For example for, having done Java, Scala, Python, Groovy, Haskell, Typescript and a couple others, Go reads extremely horrible. It feels as bad as enterprisey Java to me.

Re:Go Replaces Interface{} with 'Any' by jshen on Wednesday December 15, 2021 @01:35AM (#29562801)
I’ve used all those languages. It’s far easier to make sense of a non trivial go code base I’ve never seen before than any of those.
Re:Go Replaces Interface{} with 'Any' by valenterry on Wednesday December 15, 2021 @07:51AM (#29565269)
It's far easier for you. It definitely isn't for me or in general. Thank you for answering the list of languages though. Makes me wonder what the concrete points are for, since I really have a different opinion. Maybe our minds just work very different; :)
Re:Go Replaces Interface{} with 'Any' by mariusor on Wednesday December 15, 2021 @02:36PM (#29570594)
From the list of languages you mentioned I understand that you probably enjoy ways to express algorithms in an elegant and (maybe) terse way. Maybe that's easier for you to understand. However all that abstraction is very far removed from what the compiler or the interpreter actually execute on the CPU.

For me an easy to understand program is one where the way memory is structured and the way execution modifies said memory is easy to follow. That's why Go, despite being very verbose in some cases, it's easier to reason about, than codebases doing similar things in higher level programming languages.

Re:Go Replaces Interface{} with 'Any' by jshen on Wednesday December 22, 2021 @12:42AM (#29645958)
Just ran into a great example today. Was using typescript/javascript to hit an api that has a limit of 5 requests per second. One of these fails saying that it's making more than 5 requests per second, the other doesn't, is it obvious why? It took me several hours to figure it out, wouldn't have had this problem in go.

      choices.forEach(async (ce) => {
        let ce = choices[index]
        Deno.sleepSync(220);
        let cf = await getChoice(ce); // this makes the API call
        Deno.sleepSync(220);
      });

      for (index = 0; index < choices.length; index++) {
        Deno.sleepSync(220);
        let cf = await getChoice(ce); // this makes the API call
        Deno.sleepSync(220);
      }
Re:Go Replaces Interface{} with 'Any' by valenterry on Wednesday December 22, 2021 @03:31AM (#29646827)
Good example, because that's exactly what I don't want to see when I read code.

This should really be:

    streamOf(choices)
      .mapAsync(ce => getChoice(ce))
      .throttle(5, 1.second)
This is such a common use-case, it should really be in the streaming-library of choice. It's also a good example of how more abstract code is often better and has less edge-cases. In this example, of you have 4 choices, then these can send all at once without delay. This will be much faster compared to the code you posted, which will wait after each request, even though the rate-limit is not applied.

Apart from that, I don't think the second example is complete, where does ce come from here? And also, I don't know Deno, but calling "sleepSync" already looks like a bad idea to me, no matter where it's used - especially since calling a sync operation within an async doesn't make much sense.

Re:Go Replaces Interface{} with 'Any' by jshen on Thursday December 23, 2021 @12:08PM (#29663809)
I messed up my copy/paste, the 'let ce' line in the first example should be in the second example.

Regarding your example, where is this throttle method implemented in javascript? And, your code is not easier to reason about for me than the synchronous golang code. It's not clear how throttle is affecting the function call that happens before it. Compare to this.

    for _, v := range choices {
      getChoice(v)
      time.Sleep(200)
    }
Re:Go Replaces Interface{} with 'Any' by valenterry on Friday December 24, 2021 @11:21AM (#29674825)
> Regarding your example, where is this throttle method implemented in javascript

I didn't say it is. I said it should be. But here is a similar functionality that I just googled: https://www.learnrxjs.io/learn-rxjs/operators/filtering/thro...

I'm neither a javascript pro, nor a fan of this language. There are probably better solutions out there.

> And, your code is not easier to reason about for me than the synchronous golang code

I never said it is. If you would be familiar with only assembler, then assembly code would most easy to read. And you need to spend some time upfront to learn other languages/techniques that can make certain problems easier.

So here's the thing. Let's change the idea of this code a little bit and make it a challenge.

Let's say we want to improve the code:

1. Improve performance by sending requests as quickly as possible while respecting the rate limit. I.e. if we have 3 requests, send them all at once. If we have 7 requests, then they should all be sent after 1 second has finished.

2. If a request fails, we retry it up to 3 times and don't count it towards the api rate limit

3. If all retries for a request fail, we just skip it and continue with the rest.

I think this is a very practical real world example. I'm curious how elegant this can be solved in Go. I will also solve it and post my online-runnable solution afterwards. :-)

And then we reevaluate which solution is easier to read.

Re:Go Replaces Interface{} with 'Any' by jshen on Sunday December 26, 2021 @01:45AM (#29690317)
Here you go. Took me about 15 minutes to write, using < 50 lines of code, and not using any external libraries outside of the Go Project. The 15 minutes includes the time to create the server I used to test it.

https://gist.github.com/jaydonnell/4fd14c69132734aac76b7f538...

Re:Go Replaces Interface{} with 'Any' by valenterry on Monday December 27, 2021 @05:37AM (#29701293)
Very cool! I'll let my solution follow soon.

Immediate question that I have (since I can't execute it easily):

What happens if we have 11 requests, each request takes 1 second to be responded to, but one of the first 5 requests fails 3 times and each time the reponse until failure takes 0.1 seconds.

Will all 10 successful requests be completed within 2 seconds then? Or will the failing request block the 11th requests for 0.3 seconds, meaning that all 10 successful requests will only be completed within 2.3 seconds?

Re:Go Replaces Interface{} with 'Any' by jshen on Tuesday December 28, 2021 @01:07PM (#29716207)
I’m limiting it to 5 concurrent requests which is independent of the rate limit itself. The failing request will hold one of those 5 slots until all 3 failures are done, but won’t otherwise block anything.
Re:Go Replaces Interface{} with 'Any' by valenterry on Wednesday December 29, 2021 @04:05AM (#29723173)
I have matched this implementation, here's my approach:

https://scastie.scala-lang.org/AiusSr76QLCTrlZvBICT7A

13 lines and should behave like yours (i.e. is not optimal in the sense of fully utilizing the rate limit).

Curious to see your solution that fully utilizes the rate limit - it's not so trivial I think. :)

Anyways. I have to say that the Go-code is better than I expected (even for the suboptimal solution). But given my experience with higher abstractions (here streaming) I personally find the streaming solution much more readible.

Re:Go Replaces Interface{} with 'Any' by jshen on Wednesday December 29, 2021 @04:09PM (#29730067)
Thanks, this was a fun experiment. I was curious to see what language you used. I've done a bit of scala many years ago, and I have to say that I HATED other peoples weird and often over complex scala code. I think this is the main difference in our point of view. I 100% agree that you can write better abstractions with a language like Scala, but you can also shoot yourself in the foot far far easier. If you have a small team of very talented devs, something like scala can be amazing. At a large company, with thousands of developers of varying levels, I find you get worse results with something like scala relative to Go.

I also think that a dev who just graduated college would be able to jump into my code and work on it fairly easily. I think they'd need a lot of extra training/learning to understand yours.

One question about yours, is it using a separate OS thread for each HTTP call, or some kind of async io?

Re:Go Replaces Interface{} with 'Any' by valenterry on Thursday December 30, 2021 @05:41AM (#29735569)
You are very right and

> I also think that a dev who just graduated college would be able to jump into my code and work on it fairly easily. I think they'd need a lot of extra training/learning to understand yours.

this nails it. It's also what Go was designed for. People just shouldn't make the mistake to assume that this means would also be easier to understand a whole code base. It's not, it takes longer, since there will be so much more code. We pay the long upfront costs to learn our languages and tooling for the same reason we sent people to school for years: in the end it pays off.

> One question about yours, is it using a separate OS thread for each HTTP call, or some kind of async io?

Just like Go it uses "green threads" and will work and run requests in parallel even when just having one thread and/or cpu core.

Re:Go Replaces Interface{} with 'Any' by valenterry on Wednesday December 29, 2021 @03:54AM (#29723127)
Which means the solution isn't really optimal, since if every second request fails, the code won't take full advantage of the rate limit and only do 2.5 successful requests per second.
Re:Go Replaces Interface{} with 'Any' by jshen on Wednesday December 29, 2021 @04:06PM (#29730028)
I think the requirement is a bit vague. I'm assuming an HTTP call, and that the rate limit exists on the server. Given this, there are two different error conditions: 1) the request makes it to the server, is counted against the server-side rate limit, but fails for some reason. 2) maybe the network is down, the server never sees the request, so the failure is not counted against the rate limit.

I opted to assume all failures are of the first type, which means my code should also behave as expected, but will be slower when encountering failures of the second time.

I'm actually curious, I think it would be easier to change my code to handle both types of errors, but I could be wrong since I don't know the scala stuff you are using that well.

Re:Go Replaces Interface{} with 'Any' by valenterry on Thursday December 30, 2021 @05:51AM (#29735618)
Yeah, I guess I could have been a bit more clear. In the end it's fine, I just wanted to make it a bit more interesting and any additional constraint would have worked :)

The reason that the Scala code is so much shorter is because streams compose really well and a lot of common building blocks (such as the throttling) can be provided. This effect gets more important when the code base is growing.

Also, when things become more complicated, loops like the one you wrote tend to become very complicated. At least that has been my experience. It's fun to write it, but not so much to read or maintain it.

Re:Go Replaces Interface{} with 'Any' by jshen on Thursday December 30, 2021 @01:50PM (#29739919)
My experience has been the opposite. If I jump into a random Go code base, it’s been easier to figure out what’s going on than with languages that provide a lot more abstraction power. The reason, IMO, is that a bad abstraction is worse than less abstraction, and people are more likely to create bad abstractions than they are good ones.

I think it’s better with functional languages like scala and clojure, because bad class hierarchies with inheritance are the worst abstractions, but Go hits the right balance for me and I think for large organizations that have a lot of devs who move on and off of code bases on a regular basis.

I agree that you can get a better result with powerful languages, but that it’s unlikely in large orgs. I think software engineering at scale is more about sociology than people admit or realize.

Re:Go Replaces Interface{} with 'Any' by valenterry on Friday December 31, 2021 @10:28AM (#29748855)
I think I might agree when it comes to Java developers creating crazy class-hierarchies with no meaning. To me that is pretty much as bad as a desert of for-loops and low-level code, maybe even worse.

I'm praying for you that you at some point end up in a project with a bunch of good developers that work on a code base with a lot of good and high abstraction. The pleasure to improve your own skills in this area and feeling your productiviy rise is worth the pain of learning things to get there. It will be difficult to find this in bigger companies though.

> I think software engineering at scale is more about sociology than people admit or realize.

Yes, but it's only a matter of time. Software engineering is a growing and young field. There will be a time when we laugh about the bad code that was written, without standards and training. This time is not yet, but I'm looking forward to it. :)

Re:Go Replaces Interface{} with 'Any' by jshen on Friday December 31, 2021 @04:43PM (#29752831)
Thanks for the great convo, really enjoyed it. I have been on a project with good devs, wrote a large scale web crawler in clojure for a famous research lab. These days I’m responsible for hiring/training/retaining hundreds of devs across many code bases, some dating back decades. It gives you a whole different perspective. Also, I don’t get to code all day anymore, but I still love to code, so squeeze it in on fun hobby projects. This also gives one a whole different perspective about what is important, and what is really worth keeping in one’s head.
Re:Go Replaces Interface{} with 'Any' by valenterry on Saturday January 01, 2022 @12:16PM (#29760415)
Same here! Good luck and success with the devs :) I hope you can make the world a bit better by spreading your experience & knowledge!
Re:Go Replaces Interface{} with 'Any' by jshen on Friday December 24, 2021 @02:56PM (#29677150)
I'd love to, will code my go version after xmas. Feel free to shoot me an email, hn isn't great for long running conversations. [email protected]
Re:Go Replaces Interface{} with 'Any' by lucacasonato on Wednesday December 22, 2021 @06:31AM (#29647959)
Yeah, this should not be calling `Deno.sleepSync`... this should be an `await new Promise(resolve => setTimeout(resolve, 220));`.
Re:Go Replaces Interface{} with 'Any' by jshen on Thursday December 23, 2021 @12:09PM (#29663825)
That's kind of my point, it's less obvious how to do basic things in js/ts than golang.

What's the vanilla way in ts/js to do the equivalent of this golang code where getChoice makes an HTTP call and can't make more than 5 per second?

    for _, v := range choices {
      getChoice(v)
      time.Sleep(200)
    }
Also, why doesn't Deno.sleepSync work as I expect here?
Re:Go Replaces Interface{} with 'Any' by jshen on Wednesday December 15, 2021 @11:52AM (#29568455)
I would probably require a book length post to truly answer this, but here's the best short version I can do. I think it comes down to a few things.

1. Some languages on your list are very flexible, and that allows them to be written in a very readable form with great discipline. People tend to not have great discipline thought, for various reasons: their bosses are pushing them to go faster and cut corners, they aren't experienced enough yet, etc. I'd say that python and typescript/javascript fall into this category.

2. I think inheritance is the devil, and a more functional style is better (by functional I mean first class functions, passing parameters, etc not purely typed). Java/Groovy fall into this category, they overuse inheritance and things like dependency injection with xml or annotations rather than far easier to read function calls with parameters.

3. Then there is haskell :) Haskell requires a phd in rocket science to be proficient, and most people have other things they want to do with their life. You also can't hire a team of 1000 rocket scientists so most companies can't go this route.

Edit: One other thing I'll add about typescript/javascipt because it's a bit of an oddball. There really isn't a typescript/javascript style because most devs end up writing javascript. The java people try to write java in it, the python people python, etc. This alone makes it hard to jump into a random JS/TS codebase and figure out what's going on.

Regarding Go, it takes away a lot of flexibility, which makes the code longer generally, but it makes it much more consistent across code bases. It's lack of inheritance prevents the devil from showing up, and it's largely a simple function call with parameter passing style all the way through the code base. Structural typing is also a godsend.

Edit 2: Also, package management in python is a complete train wreck.

Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:41PM (#29562182)
Go is ok-ish, if you use small programs and solve exactly the same problems the language designers had in mind in exactly the same ways the language designers had in mind.

The constant repetition and near-but-not-quite copy-paste boilerplate everywhere makes it hard to spot the crux of what's actually going on.

Re:Go Replaces Interface{} with 'Any' by fsdjkflsjfsoij on Wednesday December 15, 2021 @02:18AM (#29562976)
I've used all of those and more and spent a decade deep in the purely functional Scala/Haskell camp. Diving into a moderate or large Go code base is always far easier for me even though I've used Go much less than the others. Before I even knew Go I would often look at various algorithms in Go just because it was so easy to understand exactly what was happening.
Re:Go Replaces Interface{} with 'Any' by valenterry on Wednesday December 15, 2021 @07:55AM (#29565301)
For algorithms, this might indeed be different. Not only are algorithms usually small and very focussed snippets, the emphasis is on performance and hence mutability, language primitives and shortcuts are common.

I can definitely see that languages like python or go beat pure functional languages in that regard.

For business logic and glue code (which in my field is the vast amount of code) I think it is the opposite though.

Re:Go Replaces Interface{} with 'Any' by jshen on Wednesday December 15, 2021 @11:41AM (#29568298)
Go is easier to read for complex code bases.
Re:Go Replaces Interface{} with 'Any' by koffiezet on Wednesday December 15, 2021 @04:59AM (#29563899)
I agree with the parent though, Go code is super-readable and accessible. I've coded actual useful stuff in Java, C, C++, Pascal, Python, PHP, Pascal, Basic, (Jenkins) Groovy, Typescript, Javascript - and am probably forgetting some.

I also dabbled around in Rust and a bunch of other languages, but I wouldn't call that experience, although when it comes down to the initial accessibility and impressions, in this context, that is relevant I think. Go was hands down the most readable and accessible of the bunch, which probably has some roots in having a C/C++ background, and it being a pretty simple language.

Dev-env wise, it used to be a mess with the GOPATH etc, but that has mostly been resolved. And once past that, it was easily the language I picked up the quickest. It took me 2 or 3 days to actually write something useful. I've jumped head-first into codebases of large projects without even thinking about it, which I would have been very hesitant about if they had been written in another language. I've had to do that plenty for C++ and Java projects, but that always took some convincing of myself, and was never a pleasant experience.

Re:Go Replaces Interface{} with 'Any' by Intermernet on Wednesday December 15, 2021 @07:36AM (#29565127)
The readability of Go is intrinsically linked to the error handling in Go. If the spec started accepting "clever" rules to do with auto-magic return / assignment of error values instead of explicit handling, then the cognitive load of code review increases. If every error path is explicit, then the review becomes simple and self-explanatory (at least when it comes to error handling).
Re:Go Replaces Interface{} with 'Any' by Zababa on Wednesday December 15, 2021 @09:15AM (#29566195)
I'm not sure error handling in Go is really explicit. Go checks that you assign an error and usually handle it, but it doesn't check that you handle all values of the error. If a function suddenly returns a new error value, the compiler won't help you here. This is like in language with unchecked exceptions, you have to read everything carefully.
Re:Go Replaces Interface{} with 'Any' by valenterry on Wednesday December 15, 2021 @07:55AM (#29565305)
By that logic, assembly would be the most simple and self-explanatory language. I'm not sure if these two attributes are sufficient to determine how readable/productive a language is in the large.
Re:Go Replaces Interface{} with 'Any' by 3a2d29 on Tuesday December 14, 2021 @11:30PM (#29562104)
Surely Python gets that honor over Go. As much as I like Go, Rust, C, over Python, I would never say the reason is readability.
Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:44PM (#29562201)
Python is quite readable for short snippets.

It can be quite a mess for larger systems.

That combination of trade-offs makes it a great language to solve interview style problems in on a white board.

You can alleviate those larger scale problems in Python a bit with good IDE support, embracing type annotations, and going with a style that prefers immutability over action-at-a-distance.

Re:Go Replaces Interface{} with 'Any' by pawelmurias on Wednesday December 15, 2021 @06:31AM (#29564527)
This change is intended to be used to avoid using interface{} with generics which aren't used by existing code
Re:Go Replaces Interface{} with 'Any' by joebob42 on Wednesday December 15, 2021 @12:27AM (#29562435)
I agree, and I think this mistake is at the root of most of what are (at least to my biased point of view) the big mistakes made by go. Syntactical niceties and simplicity / obviousness are sometimes a tradeoff, but go 1.0 felt like the designers just folded their arms and refused to consider any niceness. I still don't understand why anyone would use go when there are so many better choices, but at least they're fixing some stuff now :)
Re:Go Replaces Interface{} with 'Any' by valenterry on Tuesday December 14, 2021 @08:34PM (#29560899)
> I thought that using an empty interface to represent "any type" was kind of ingenious

It makes me a bit sad to read that. We all are on a journey to be become better developers every day. But things like that are like a distraction. They make you think you found a really cool and smart concept, but you actually didn't and it's essentially just a hack.

Not sure what the solution can be. But PL designers should be more clear about features that are just "hacks" and considered "bad" but necessary in practice due to certain constraints. Go's {} and nil-errorhandling are examples. Nulls (in any language) are another common example.

Re:Go Replaces Interface{} with 'Any' by laumars on Wednesday December 15, 2021 @02:38AM (#29563086)
That’s a very condescending way of say “I don’t personally like this feature”.
Re:Go Replaces Interface{} with 'Any' by the-alt-one on Wednesday December 15, 2021 @03:11AM (#29563246)
It's not a hack, it's the natural way of expressing it in the chosen type system. The use cases for interface{} are often hacks, because of a lack of parametric polymorphism, but that's different.
Re:Go Replaces Interface{} with 'Any' by Waterluvian on Tuesday December 14, 2021 @09:12PM (#29561202)
This is valuable for a learning language and for learning in general. But it’s the kind of concept that ought to be abstracted away, in my opinion.
Re:Go Replaces Interface{} with 'Any' by egeozcan on Tuesday December 14, 2021 @05:22PM (#29558910)
I kind of understand what you mean, as it's similar to what happened with prototypes and the late-comer class syntax in JavaScript, and in that case, it was a net positive change as most would probably say.

Let's see how this plays out.

Re:Go Replaces Interface{} with 'Any' by nightowl_games on Wednesday December 15, 2021 @08:01AM (#29565343)
I'm an experienced programmer whos just learning go now and I never made the connection that interface{} meant 'an interface with no methods'. I've always thought 'interface{}' was the clearest wart on the language. Languages shouldn't be about teaching me things, they are just a tool. Keyboard input goes in, computer instructions come out. any is easier to read, more intuitive in its meaning and takes less keyboard input.
Re:Go Replaces Interface{} with 'Any' by chmod775 on Wednesday December 15, 2021 @05:46AM (#29564189)
> Learning Go really reframed my concept of what an interface is, and I thought that using an empty interface to represent "any type" was kind of ingenious

There's a few other languages out there that work that way. The split is probably is probably 20/80. Though "popular" languages heavily learn towards Java-esque interfaces - with TypeScript and Go being the odd ones in that bunch.

Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:37PM (#29562153)
> It's definitely more streamlined, but my concern would be that newcomers might not understand that it's just an alias.

Having lots of syntactic sugar works out OK for Haskell.

For example, in Haskell straight-line imperative looking code is an alias for some underlying callback hell.

Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @04:53PM (#29558465)
I cannot wait for the Result monad.

The state of error handling in Go at the moment is embarrassing at best.

Re:Go Replaces Interface{} with 'Any' by the_duke on Tuesday December 14, 2021 @05:04PM (#29558638)
Emulating sum types in languages without pattern matching is extremely awkward, to the point of being almost useless.

type Result[T] struct { ok: *T err: error }

Great, so how do you work with it?

* You can have a `ok()` getter that returns `*T`. Now you you need an `if x != nil`

* `isOk()` + `isErr()`

* `unwrap() T`, which panics on errors

* `split() (*T, err)` that splits into separate values, especially awkward since you both need `if err != nil` AND dereference the pointer

That API is more awkward then the status quo, and doesn't buy you any correctness guarantees because eventually you have to do an `if x := res.ok(); x != nil` or `x, err := res.split(); if err != nil {` anyway.

Pretty much the only convenience you gain are functions like `map()` or `unwrap_or`, but eventually you always have to take out the value and without being able to pattern match you can't get an API that improves correctness much.

(std::optional in C++ is a great example)

Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @05:16PM (#29558825)
The Rust Result type has:

  - map / map_err
  - and_then / or_else
  - unwrap / unwrap_or
And countless of other functions making it very practical to chain computations without having to pattern match anything.

I do the same in Erlang/Elixir.

In Golang, I need to check every function call, and if I want to know where an error come from, I need to wrap it in an errors.New() because no exceptions = no stacktrace

Re:Go Replaces Interface{} with 'Any' by kbd on Tuesday December 14, 2021 @05:22PM (#29558916)
> and if I want to know where an error come from, I need to wrap it in an errors.New() because no exceptions = no stacktrace

Zig manages to provide traces with very little overhead:

https://ziglang.org/documentation/master/#Error-Return-Trace...

I am baffled as to why error handling in Go remains so impoverished.

Re:Go Replaces Interface{} with 'Any' by mariusor on Wednesday December 15, 2021 @02:46PM (#29570691)
Stack traces exist in go[1], I'm not sure why you consider exceptions to be the only way of getting a stack representation.

Not to mention that traditional exception handling advice I've been handed down from the gray beards is to always handle exceptions as early as possible, which is exactly what go forces you to do with their approach.

[1] https://pkg.go.dev/runtime/debug#Stack

Re:Go Replaces Interface{} with 'Any' by preseinger on Wednesday December 15, 2021 @02:42AM (#29563103)
Chaining computations hides the error control flow path, which negatively impacts readability.
Re:Go Replaces Interface{} with 'Any' by DarylZero on Tuesday December 14, 2021 @08:24PM (#29560797)
In general, you can replace pattern matching with a function that takes one function for each constructor of the sum type.

However, doing this kind of ... sucks.

Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:48PM (#29562221)
It sucks when you have to use it for everything, but it's a useful enough technique that most Haskell datatypes come with functions that provide exactly this conversion to church encoding.

See eg 'maybe' (https://www.stackage.org/haddock/lts-18.18/base-4.14.3.0/Pre... on stackage) or foldr for lists.

'foldr' is interesting, because it encapsulates a recursive pattern matching on lists. For the non-recursive version, see 'uncons' composed with 'maybe'.

Re:Go Replaces Interface{} with 'Any' by DarylZero on Wednesday December 15, 2021 @01:36AM (#29562806)
It's probably actually better than pattern matching when there are only two, as in those cases. But even at three, it becomes a mess. And more than three? Yuck.
Re:Go Replaces Interface{} with 'Any' by eru on Thursday December 16, 2021 @03:58AM (#29576382)
If your language does currying similar to Haskell, and there's a good order to your constructors / arguments, even with three or more there might be some benefits with partial application.
Re:Go Replaces Interface{} with 'Any' by egeozcan on Tuesday December 14, 2021 @05:24PM (#29558950)
I don't see why that wouldn't be possible to implement now that the generics are here.
Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @05:27PM (#29558975)
Which is exactly why I said:

> I cannot wait for the Result monad.

Generics make this possible, and will be a huge improvement to Go's error handling.

I'm not saying it will solve everything, but it's a huge step nonetheless.

Re:Go Replaces Interface{} with 'Any' by xyzzy_plugh on Tuesday December 14, 2021 @07:16PM (#29560114)
I think you will find that Go's error handling will not change as much as you suggest. The majority of developers are extremely cautious of generics and found the status quo to be the best balance between readability and conciseness.

Convenience is not a goal of the language, generally.

Re:Go Replaces Interface{} with 'Any' by saghm on Wednesday December 15, 2021 @01:29AM (#29562775)
> The majority of developers are extremely cautious of generics and found the status quo to be the best balance between readability and conciseness.

Most people who use Go have specifically chosen to use it though (or at least sought out opportunities, given that it's not something entrenched like Java being used due to inertia), so yes, most of them are fine with how Go is or they wouldn't be using it in the first place. If Go had generics from the start, I'd imagine most users would be fine with that too due to self-selection.

Re:Go Replaces Interface{} with 'Any' by Aeolun on Tuesday December 14, 2021 @11:16PM (#29562029)
It doesn’t need to be part of the core language. Everyone can make their own monad, or use the one from a library.
Re:Go Replaces Interface{} with 'Any' by egeozcan on Tuesday December 14, 2021 @06:01PM (#29559333)
I seem to have lost track of how the discussion reached that point, thanks for the clarification.
Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:54PM (#29562259)
Depends on how generic you want this.

In Haskell, you can have functions that work generically over option-types, error-able types, lists, functions, tuples, etc.

In Rust, you have to specifically implement functionality for all of these.

But eg your 'map' function in Rust still works for all lists, no matter what item type. In Go before this change, you had to write a different map function for each item type that could be in your list.

In Haskell, the same 'map' function works for lists, functions, error-able types etc.

Re:Go Replaces Interface{} with 'Any' by johnisgood on Tuesday December 14, 2021 @08:37PM (#29560933)
Huge no to anything that will require .unwrap() noise everywhere in the codebase. I use Erlang, my code does not have ".unwrap().unwrap()"[1] anywhere.

[1] https://github.com/SeaQL/sea-orm/blob/64c54f8ad603df0c1d9da8...

Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:50PM (#29562237)
That's actually a problem in Erlang.

In Haskell, 'Maybe (Maybe Int)' is a different type from 'Maybe Int'.

That means that when you use eg a hash table that returns some kind null value like 'Nothing' on lookup when a key is not found, you can still stick exactly that kind of null value as a normal value into the table and everything will turn out fine.

Re:Go Replaces Interface{} with 'Any' by TheDong on Tuesday December 14, 2021 @10:22PM (#29561693)
The comparison here is with go.

Unwrap would look like:

    file := os.Open("foo").Unwrap()
I'll take that over the current go state of the art:

    file, err := os.Open("foo")
    if err != nil {
        panic(err)
    }
Just like "panic(err)" is used infrequently, "unwrap" would be used infrequently. They're comparable, and for the cases where unwrap is okay (test code, once-off scripts, etc), I'd definitely prefer it to the panic boilerplate.
Re:Go Replaces Interface{} with 'Any' by tptacek on Tuesday December 14, 2021 @11:08PM (#29561977)
I don't understand this example at all. A Rust programmer would essentially never use unwrap on the return from File::open. It would be like checking open(2) with assert() in C.
Re:Go Replaces Interface{} with 'Any' by preseinger on Wednesday December 15, 2021 @02:43AM (#29563120)
That Go would never pass code review unless it is part of a tiny CLI tool or script.
Re:Go Replaces Interface{} with 'Any' by imron on Tuesday December 14, 2021 @09:37PM (#29561396)
I use rust and my code does not have .unwrap() (and definitely not .unwrap().unwrap()) in it either. That's what the ? operator is for.
Re:Go Replaces Interface{} with 'Any' by eru on Tuesday December 14, 2021 @11:51PM (#29562247)
The '?' is not a conceptual advance over .unwrap(), it's "just" a major syntactic convenience.
Re:Go Replaces Interface{} with 'Any' by ComputerGuru on Wednesday December 15, 2021 @02:10AM (#29562947)
Of course it is. Unwrap panics on error, while ? bubbles up the error instead. But ? wasn’t introduced to replace unwrap, unwrap was always frowned upon. It instead replaces match and return.
Re:Go Replaces Interface{} with 'Any' by imron on Wednesday December 15, 2021 @04:17AM (#29563662)
It was always frowned upon, but still ended up everywhere (including lots of documentation and example code) because it was less verbose than the match handling. The ? helped remove that last obstacle.
Re:Go Replaces Interface{} with 'Any' by kbd on Tuesday December 14, 2021 @05:14PM (#29558798)
> because eventually you have to do...

Emphasis on eventually instead of at every single function call.

Re:Go Replaces Interface{} with 'Any' by valenterry on Tuesday December 14, 2021 @10:04PM (#29561577)
> but eventually you always have to take out the value

No. You can just feed it to the function (from a library / stdlib) that needs it, or call .fold() in the end.

Re:Go Replaces Interface{} with 'Any' by peoplefromibiza on Tuesday December 14, 2021 @05:34PM (#29559055)
you can map on ok which is an alias for map_ok and then map_err for errors and or_else or_throw etc.

that's similar to what Java does with the Optional type, not great, but not bad either

the alternative is checking for nulls which is worse in any possibile way

I usually implement something like Kotlin Result when I have to code in Java

with a couple of static helpers to build the result: Result.success(T) Result.failure(Throwable t)

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/

also Result in Rust has been an inspiration

https://doc.rust-lang.org/std/result/enum.Result.html

Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @06:13PM (#29559474)
I also made a library for working with `{:ok, value}` and `{:error, reason}` in Elixir: https://github.com/linkdd/rustic_result

Thanks to the pipeline operator and pattern matching, it makes pretty easy to read pipelines. It does not completely replace the with statement (that was not the point) but it simplified a lot of code.

Re:Go Replaces Interface{} with 'Any' by peoplefromibiza on Tuesday December 14, 2021 @06:42PM (#29559752)
this is nice, especially for the function clauses

Elixir is my daily drive and working in Java made me miserable until I started working around the lack of pattern matching facilities

Error handling is the worst part, I think a simple

  switch (retval) {
    Ok(val):
       ...
       break;
    Error(err):
       ...
would make Java much more pleasant, even without full pattern matching everywhere.
Re:Go Replaces Interface{} with 'Any' by crdrost on Tuesday December 14, 2021 @10:25PM (#29561711)
This sorta exists in Java 17:

    sealed interface Result<T> 
        permits Ok, Error {}
    record Ok<T>(value: T) implements Result<T> {}
    record Error<T>(error: Throwable) implements Result<T> {}
    
    // to consume
    switch (result) {
      case Error e -> e.error().getMessage(),
      case Ok v -> v.value().toString()
    }
Re:Go Replaces Interface{} with 'Any' by kaba0 on Tuesday December 14, 2021 @08:01PM (#29560577)
Nitpick, but I like to point out that Java’s exceptions are themselves a Result type/analog with them. You can unwrap it by a try catch, and can choose to rethrow it with minimal syntax. It even has a plus, it will auto-attach a stacktrace to the error case.
Re:Go Replaces Interface{} with 'Any' by absove on Wednesday December 15, 2021 @06:02AM (#29564307)
Not so much a nitpick because with checked exceptions your method signature can declare multiple types of exception whereas Result generalizes your specific exceptions to Throwable, which you can't pattern match with the compiler being able to enforce exhaustiveness.

So to achieve the same exhaustiveness as a method like

User createUser() throws IOException, BusinessException

you would need both a Result-like construct and union types

Either<User, IOException | BusinessException> createuser()

which few mainstream languages offer. Otherwise you type is unable to express that only IOException and BusinessException are thrown and you don't need to handle a default Throwable case.

Re:Go Replaces Interface{} with 'Any' by peoplefromibiza on Wednesday December 15, 2021 @08:00AM (#29565331)
checked exceptions don't integrate very well with streams and reactive pipelines, to the point that basically all of then end up being wrapped and rethrown as unchecked

methods that declare checked exception can throw RuntimeException(s) so exhaustiveness cannot be totally enforced anyway

Re:Go Replaces Interface{} with 'Any' by saghm on Wednesday December 15, 2021 @01:37AM (#29562810)
This is only the case for "checked" exceptions though; the important part about the Result type in Rust is that it won't implicitly propagate errors if you try to just use the success value but instead will give a compiler error. I don't code much Java, but my impression is that standard library methods throwing unchecked exceptions is far more common than panicking, the closest equivalent is in Rust.
Re:Go Replaces Interface{} with 'Any' by tptacek on Tuesday December 14, 2021 @06:12PM (#29559457)
A lot of people are going to write very un-idiomatic Go code with "Result" types built on simple generics that, 5 years from now, we'll all be kind of smirking at. Rust's Option and Result make sense because the language supports it (most importantly with match statements). They don't make sense here.
Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @06:16PM (#29559499)
Fun observation of mine: most articles/tutorials I've read about "idiomatic go code" are presenting ideas that you never see in any production code.

Seems to me that the ones who write about "idiomatic go code" aren't the ones who are shipping softwares/libraries.

Re:Go Replaces Interface{} with 'Any' by tptacek on Tuesday December 14, 2021 @07:11PM (#29560063)
This seems pretty glib. There's pretty obviously a phenomenon where people try (or used to try) to write code for other languages (less commonly Rust, more commonly Ruby) in Go, and that code is kind of notoriously uncooperative with the rest of the ecosystem and hard to maintain. You'll find it in Go web stack libraries, in particular, or with code for people who tried to fake generics with codegen.
Re:Go Replaces Interface{} with 'Any' by preseinger on Wednesday December 15, 2021 @02:47AM (#29563140)
Example?

The majority of Go I've been hired to improve has suffered immensely from not using accepted idioms. Most of my work has been focused on introducing those idioms systemically.

Re:Go Replaces Interface{} with 'Any' by nojito on Tuesday December 14, 2021 @06:55PM (#29559887)
This is true of all developer evangelism in general.
Re:Go Replaces Interface{} with 'Any' by Thaxll on Tuesday December 14, 2021 @05:06PM (#29558672)
Well it's better than most language with exceptions.

People makes it a big deal, in reality it's not.

Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @05:18PM (#29558841)
An exception has a stacktrace. This single piece of information is crucial when you debug and makes handling errors in Golang embarrassing.

I rest my case.

Re:Go Replaces Interface{} with 'Any' by geodel on Tuesday December 14, 2021 @07:35PM (#29560311)
Yea, I am not really fan of 1000 line stack trace for an error in single line. I like Go's error handling better. Also it is clear you like error handling in Rust/Java etc which is fine. Exaggerating over how bad error handling in Go is hardly productive when a) many people do like it b) people can switch language when error handling primary concern over everything else.
Re:Go Replaces Interface{} with 'Any' by kaba0 on Tuesday December 14, 2021 @08:02PM (#29560590)
How will you fix that bug in production when you can’t reproduce it and only has logs? Stacktraces are godsend.
Re:Go Replaces Interface{} with 'Any' by geodel on Tuesday December 14, 2021 @08:49PM (#29561039)
Well as I said people do like stack traces. Hell, I work on language which just has exception stack traces as error handling. I find it hit and miss not godsend when fixing production issues.
Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @06:17PM (#29559506)
Go exceptions do carry stack trace information.

However, the topic is errors, not exceptions. Those are very different concepts. Of what use is stack trace information in debugging values that you have assigned error meaning to when not other types of values?

If you had a function

   func add(a, b int) int { return a * b }
there would be no expectation of carrying a stack trace to debug it. So what's different about

   func add(a, b int) error { return errors.New("cannot add") }
that does require a stack trace?
Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @06:30PM (#29559611)
Current state of the art:

  func read_file(filename string) (string, error) {
    return "", errors.New("oops")
  }

  func foo() error {
    a, err := read_file("a.txt")
    if err != nil {
        return errors.New(fmt.Sprintf("read a: %s", err))
    }

    b, err := read_file("b.txt")
    if err != nil {
        return errors.New(fmt.Sprintf("read b: %s", err))
    }

    // do stuff with a and b
    return nil
  }

  func main() {
    err := foo()
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
  }
In a language with exceptions:

  func read_file(filename string) string {
    throw FileNotFound(filename)
  }

  func foo() {
    a := read_file("a.txt")
    b := read_file("b.txt")
    // do stuff with a and b
  }

  func main() {
    try {
      foo()
    }
    catch (err FileNotFound) {
      fmt.Fprintf(os.Stderr, "file not found: %s\n%s\n", err.filename, err.Stacktrace())
    }
  }
With the current go error handling, you need to add the informations yourself in the string, not as a real data structure.

And before you say "you can add the filename to the error message in read_file()", what if the function is defined in a dependency you have no control over?

An exception is a typed data structure that contains way more informations and value to automate rescuing.

Delegating error handling to a try/catch block with a typed data structure allows the caller to care for certain type of errors and delegate the others to its own caller. With the current error type in Go, what would you do? parse the error message?

Re:Go Replaces Interface{} with 'Any' by tptacek on Tuesday December 14, 2021 @07:14PM (#29560099)
This is definitely not the current state of the art. Errors are values with types just like anything else in the language; dynamically populating calls to errors.New() is super weird (the point of errors.New is mostly to create global errors you can compare against), your errors nest without using %w, and there are a bunch of simple libraries that wrap typed stack traces in errors, just like there are a bunch of simple libraries in Rust that exist to make error types compatible with each other.
Re:Go Replaces Interface{} with 'Any' by benburwell on Tuesday December 14, 2021 @09:45PM (#29561441)
Your "state of the art" Go code is not really a good example of how that functionality would be written.

If you want to check for a specific error condition, then just define a value for that error and use `errors.Is` to check for it. This works as you'd expect with wrapping: https://go.dev/play/p/rJIlKKSYn9Q

> With the current go error handling, you need to add the informations yourself in the string, not as a real data structure.

This is completely false! If you want to provide a structured error, then you just need to define a type for it. In your example, a Go programmer might use errors.Is(err, fs.ErrNotExist) and errors.As if they wanted to retrieve the specific file path that does not exist in a strongly-typed way, something like https://go.dev/play/p/hdHPLAVbQuW.

> Delegating error handling to a try/catch block with a typed data structure allows the caller to care for certain type of errors and delegate the others to its own caller. With the current error type in Go, what would you do? parse the error message?

Certainly not! I think there is a misconception that "an error is a string" -- in Go, an error is actually any type that satisfies the error interface, i.e. has an `Error() string` method. It can be any type at all, and have as many other methods as you like in order to provide the functionality you need.

> what if the function is defined in a dependency you have no control over?

There's nothing stopping you from writing `throw new Exception(String.format("file not found: %s", filename))` in languages with exceptions either. In both cases, it would be recognized as poor API design.

Regarding stack traces, Go makes a strong distinction between errors (generally a deviation from the happy path) and panics (a true programming error, e.g. nil pointer dereference, where the program must exit). Errors do not provide stack traces since there is no need for them in a flow control context, panics do provide stack traces for useful debugging information.

Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @06:34PM (#29559666)
Your code is wrong. It would normally be written something like this:

  func read_file(filename string) string {
    panic(FileNotFound{filename})
  }

  func foo() {
    a := read_file("a.txt")
    b := read_file("b.txt")
    // do stuff with a and b
  }

  func main() {
    defer func() {
      if err, ok := recover().(FileNotFound); ok {
        fmt.Fprintf(os.Stderr, "file not found: %s\n%s\n", err.filename, err.Stacktrace())
      }
    }

    foo()
  }
However, exceptions are meant for exceptional circumstances (hence the name), not errors. A file error is not exceptional in the slightest. It is very much expected.

While you can overload exceptions to pass errors (or any other value), that does not mean you should. Your use of exceptions for flow control (i.e. goto) is considered harmful.

Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @06:48PM (#29559821)
Ok I was wrongly assuming that panic was expecting an error type, in fact it's an interface{}.

> Your use of exceptions for flow control (i.e. goto) is considered harmful

Exceptions are a way to delegate error handling to the caller by giving them informations about the unexpected behavior. It implies that the expected behavior is the "happy path" (everything went well) and any deviations (errors) is unexpected.

This is far from a goto because you can have `try/finally` blocks without catch (or defer in golang).

Also, exceptions are just a kind of algebraic effects that do not resume. There was a proposal to JS for this: https://github.com/macabeus/js-proposal-algebraic-effects

This is also easier to test. assertRaises(ErrorType, func() { code... })

Almost every Go library I've seen just return an error (which is just a string), you'd need to parse it to assert that the correct error is returned in special conditions.

Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @06:55PM (#29559882)
> It implies that the expected behavior is the "happy path" (everything went well) and any deviations (errors) is unexpected.

Errors are the "happy path", though. Your network connection was lost for the data you were trying to transmit so you saved it to your hard drive instead means that everything went well! Throwing your hands up in the air and crashing your program because you had to make a decision is not something you would normally want to do. If statements are present in most happy paths for good reason. That the inputs presented you with a choice does not remove you from the happy path.

Now, if you made a programming mistake and tried to access an array index that is out of bounds, then there isn't much else you can do but crash. Exceptions are appropriate for that kind of problem. They are exceptional in that they should never happen. Errors, on the other hand, are expected to happen and you can happily deal with them.

Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @07:06PM (#29560001)
> Throwing your hands up in the air and crashing your program because you had to make a decision is not something you would normally want to do

And that's not something you do thanks to try/catch. You just handle the error where it's meaningful to handle it.

The happy path of "make a request" is that there is no network error.

The happy path of "make sure this request is sent" is that you handle the unexpected network error to save the request to disk for further retry.

If the disk is full, you're not on the happy path anymore.

In Erlang/Elixir, there is a philosophy of "let it crash" which is basically "delegate the error handling/recovery to where it's meaningful to do so". For example:

  start a supervised process to send a request
  if there is an unexpected network failure, let it crash
  the supervisor retries on its own
Or:

  start a process to send a request
  if there is an unexpected network failure, let it crash
  monitor the process to be notified when it crash
  do something if it happens, like saving the request to disk
A "write_file" function can return many kind of errors:

  - file not found (some folder in the path does not exist)
  - permission denied
  - disk full
When you call write_file, you might want to handle some of those errors, and delegate the handling of others to your caller.

You're still not addressing my main point. How do you check which errors you want to handle and which one you want to propagate with just a string describing your error ?

Re:Go Replaces Interface{} with 'Any' by tptacek on Tuesday December 14, 2021 @07:16PM (#29560112)
You don't. That's why Go errors are typed. You use `errors.Is` to check if any of the errors in a chain of wrapped errors is of a particular kind. It doesn't grovel through strings to answer that question.

It is very weird that this subthread starts with "current state of the art".

Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @10:49PM (#29561862)
> And that's not something you do thanks to try/catch. You just handle the error where it's meaningful to handle it.

And it is always most meaningful to handle it immediately, so what's the point of introducing a application-wide goto jump, amid Dijkstra's warnings that doing so is harmful when you're just going to catch right away anyway?

> The happy path of "make a request" is that there is no network error.

If there is a network error, you're still happy. It is not like you screwed up as a programmer. What is there to be unhappy about? The network error input to your function is very much expected and part of the "happy path" as much as any other input to your application.

> if there is an unexpected network failure, let it crash

Network failures are never unexpected. It would be exceptional to never experience a network failure.

> How do you check which errors you want to handle and which one you want to propagate with just a string describing your error?

Why would your errors be strings? When errors are just plain old values like any other it is true that you could resort to using strings to represent errors, but it would be quite unidiomatic to do so. Kind of like how you can overload exceptions to handle errors, but just because you can does not mean you should.

Re:Go Replaces Interface{} with 'Any' by dragonwriter on Tuesday December 14, 2021 @06:19PM (#29559522)
> However, the topic is errors, not exceptions.

Idiomatically, go uses errors for the purposes other languages use exceptions, so if this makes debugging harder, it's an important consideration.

Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @06:30PM (#29559614)
That is also true of most languages. Java (and Javascript in its attempt to copy it) are about the only languages that actually promote using exceptions for errors, and in hindsight I think we can agree it was a poor design decision. That doesn't stop people from trying to overload exceptions in other languages, Go included, but in terms of what is idiomatic...

However, the question was asking what is different about errors compared to other values. The add function above contains an error, yet I don't know of any language in existence where you would expect a stack trace bundled alongside the result to help you debug it. Why would that need change just because you decided to return a struct instead of an int? And actually, many APIs in the wild do represent errors as integers.

Re:Go Replaces Interface{} with 'Any' by dragonwriter on Tuesday December 14, 2021 @06:46PM (#29559785)
> Java (and Javascript in its attempt to copy it) are about the only languages that actually promote using exceptions for errors

Python does. Ruby does. It's not just Java and JS. Go is very open about its approach being a departure.

> And actually, many APIs in the wild do represent errors as integers.

Many, many APIs in the wild are implemented in (or meant to be consumed from) C, which doesn't even have exceptions, so not using exceptions makes sense for them.

Very often idiomatic non-C host language wrappers for those APIs will fire exceptions when they get an error return.

Re:Go Replaces Interface{} with 'Any' by kortex on Tuesday December 14, 2021 @08:26PM (#29560814)
> Python does

And it's awful. I use EAFP locally (to avoid TOCTOU and the like) at low level interfaces but I don't let it bubble up out of a function scope, because it is a goto in all but name.

I've also been increasingly using the `result` library/data structure. It's incredibly liberating to return an error as an object you can compose into other functions, vs try/catch, which does not compose.

Yes I write python almost like rust, and it's great. Strong types, interfaces, immutable types. It looks nothing like "old school python" but also behaves nothing like it. Gone are the day of "oh it crashed again, fix one line and rerun".

Exceptions should be for exceptional circumstances, not errors.

Edit: I see this is controversial. What do you take objection to? Making your python look less dynamic and more like rust? Try it before you knock it. Python's my favorite language, but I do not agree that many of the common "pythonic" patterns are good at scale.

https://returns.readthedocs.io/en/latest/pages/result.html

Re:Go Replaces Interface{} with 'Any' by Karunamon on Wednesday December 15, 2021 @10:55AM (#29567712)
Maybe I'm just too inexperienced, but I don't see the point of this library.

The linked example shows a really basic sqlalchemy model lookup. What does spewing these new types all over my code get me that returning None or an empty dict/list doesn't without the overhead?

    def find_user(user_id: int) -> Optional[User]:
      user = User.objects.filter(id=user_id)
      if user.exists():
        return user[0]
      else:
        return None
Not only is this idiomatic, it conveys the same semantic meaning. I'm using an IDE, as is anyone else working in a large codebase. I'll be told at the point of invocation that find_user could return None and I need to possibly deal with that.
Re:Go Replaces Interface{} with 'Any' by dragonwriter on Wednesday December 15, 2021 @12:28PM (#29568974)
> Not only is this idiomatic, it conveys the same semantic meaning

No, it doesn’t. For a single computation like this, that pattern is roughly equivalent to Maybe (which contains no information about the case where there is no success result besides that it is absent) rather than Result (which has error information, kind of like an exception, but in the normal return path.)

For a series of computations, the composability of both Maybe and Result means that they are semantically richer.

Re:Go Replaces Interface{} with 'Any' by Karunamon on Wednesday December 15, 2021 @01:00PM (#29569350)
Couldn't I just return a tuple instead if I wanted to give the caller information on the nature of the success or failure?

Also, both Django and SQLAlchemy throw proper exceptions on bad queries or DB errors, which is probably the right thing to do in the average app using these libraries (the exception bubbles up, getting logged, returning the appropriate http error, etc).

I'm not crapping on this library, mind, I just can't find the use case that justifies it.

Re:Go Replaces Interface{} with 'Any' by linkdd on Wednesday December 15, 2021 @01:37PM (#29569892)
It's about function composition.

How do you compose a function that returns an int or an error with a function that takes an int as parameter?

Yes you can split this in multiple steps, or you can use monads to handle the composition for you, making you type less code, giving more information to the typesystem (mypy for python for example) about what is valid and what's not.

This is a completely different programming style, it's functional programming, aka: "how to make functions by composing other functions".

Re:Go Replaces Interface{} with 'Any' by eru on Wednesday December 15, 2021 @12:02AM (#29562296)
I find, that the problem with exceptions in Python is not so much that it is a goto, but that they are a bit like dynamic scoping (vs static scoping of variables), so you can not reason about them statically.

An example: suppose you have a function that does some work with the filesystem, and also calls some user-supplied code. (Perhaps because the user can subclass something etc, or because you are getting a callback, the details don't matter.)

Naturally your function might have some idea how to handle its own filesystem trouble, but you have no clue how to handle any filesystem exceptions that come from user provided code.

It's rather awkward to get this exactly right.

Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @07:00PM (#29559937)
> Ruby does.

Definitely not. Especially because early Ruby implementations brought huge overhead when exceptions were used, you were strongly advised to only use exceptions for actual exceptions. Ruby was one of the first languages that really started pushing the idea that exceptions should be reserved for exceptions, even if was just for technical reasons.

Those overhead problems have been addressed and are no longer a problem, but the sentiment has continued to ring true. I agree that doesn't stop people from trying to overload them, as I said earlier. But idiomatic? Not at all.

Re:Go Replaces Interface{} with 'Any' by rezonant on Wednesday December 15, 2021 @12:42AM (#29562512)
Ruby has two kinds of exception handling. There is try/catch, this is used for flow control and should not be used for error handling (it doesn't have stack traces). And then there is raise/rescue.

Using try/catch as intended is a bit of an art, but raise/rescue is everywhere. It is absolutely the primary way of handling errors. I think you might be confusing these two.

Re:Go Replaces Interface{} with 'Any' by pkaye on Tuesday December 14, 2021 @05:57PM (#29559285)
Stack trace was part of a proposal to add to the error package but it didn't happen. You can use third party errors packages like https://github.com/pkg/errors which wrap errors with stack traces.
Re:Go Replaces Interface{} with 'Any' by mseepgood on Tuesday December 14, 2021 @05:42PM (#29559141)
If you want stacktraces just panic. But that's not proper error handling.
Re:Go Replaces Interface{} with 'Any' by divan on Tuesday December 14, 2021 @06:58PM (#29559922)
Or just print stacktrace with `debug.PrintStack()`
Re:Go Replaces Interface{} with 'Any' by papito on Tuesday December 14, 2021 @05:13PM (#29558796)
People downvote it, but it's true. 2/3 of your Go code is `if err` blocks. And the way you have to chain the error messages to make the stack make any kind of sense is just maddening.
Re:Go Replaces Interface{} with 'Any' by Vanclief on Tuesday December 14, 2021 @05:27PM (#29558979)
I used to really hate that. After reading this amazing post https://middlemost.com/failure-is-your-domain I created a module that makes this bearable https://github.com/Vanclief/ez
Re:Go Replaces Interface{} with 'Any' by neop1x on Wednesday December 15, 2021 @03:26AM (#29563336)
We often have errors handled. In contrast to many Java codes just spitting unhandled exception stacktraces or crashing completely.
Re:Go Replaces Interface{} with 'Any' by papito on Wednesday December 15, 2021 @09:12AM (#29566150)
You must handle an exception in a language like Java. Yes, you can just print the stacktrace, but that is a choice. I don't HAVE to handle errors in Go until the program crashes as well. I never really understood that argument.
Re:Go Replaces Interface{} with 'Any' by rp1 on Tuesday December 14, 2021 @06:03PM (#29559363)
I’ve been doing a lot of rust programming recently, but how exactly is the Result monad better? I feel like I end up with nested match statements for chained results, but maybe I’m doing something wrong.
Re:Go Replaces Interface{} with 'Any' by linkdd on Tuesday December 14, 2021 @06:07PM (#29559408)
Rely more on the map/map_err/or_else/... methods for the Result type. You'll get something like (pseudocode):

  may_fail_who_knows()
    .map(use_value)
    .map_err(some_error_processing)
    .and_then(another_computation_which_can_fail)
    .or_else(with_some_error_handling_that_can_rescue)
    .unwrap_or(a_default_value)
Basically, instead of nested match expressions, you get a "pipeline".
Re:Go Replaces Interface{} with 'Any' by wmanley on Tuesday December 14, 2021 @07:06PM (#29560003)
This is equivalent to:

    let x = match may_fail_who_knows() {
        Ok(y) => Ok(another_computation_which_can_fail(use_value(x))),
        Err(e) => with_some_error_handling_that_can_rescue(
            some_error_processing(e)),
    };
    match x {
        Ok(y) => y,
        _ => a_default_value,
    }
It's a bit more verbose than using the combinators, but someone coming across it for the first time will understand it immediately because there's less to remember to understand it (this is where go really shines).

Also: by avoiding functors there are fewer subtle lifetime issues and `move ||` stuff to deal with and you can return from the containing function and use the `?` operator.

During the discussions of how `.await` was going to work for rust async there was the proposal to add other suffix keywords. So this would look like:

    may_fail_who_knows()
    .match {
        Ok(y) => Ok(another_computation_which_can_fail(use_value(x))),
        Err(e) => with_some_error_handling_that_can_rescue(
            some_error_processing(e)),
    }
    .match {
        Ok(y) => y,
        _ => a_default_value,
    }
Maybe not that different.
Re:Go Replaces Interface{} with 'Any' by valenterry on Tuesday December 14, 2021 @10:10PM (#29561608)
> This is equivalent to

You just had to write the concrete types (Ok and Err) out. What if these types are changed later on, e.g. to "Some(...)" and "None" or "Ok" and "ManyErrs(...)"?

As you said, it is easier to understand. Because it less abstract. This can be a good thing, but as well be a bad thing - but one thing is sure: while it does the same in the concrete case, the code is not "equivalent" when it comes to refactoring and certain changes.

Re:Go Replaces Interface{} with 'Any' by wmanley on Wednesday December 15, 2021 @08:22AM (#29565530)
> the code is not "equivalent" when it comes to refactoring and certain changes.

Yes, there are a very specific and limited set of changes you could make to the types here and not have to change this code. You can't replace `Result<>` with `Option<>` because of `map_err`. You could replace `Result<>` with something else that is very `Result<>`y, but your flexibility would be very limited if you didn't want to change the signatures of `with_some_error_handling_that_can_rescue` or `some_error_processing`.

I'm sure it's possible to contrive an example where this would help, but I don't believe that it would be that much of a help very often in practice. I think it's just a bit more monady and people who take the time to learn monads then want to apply that wherever they can.

I'm not saying that the combinators should never be used, but that each additional one you use increases the cognitive burden of reading your code. So the question becomes: which of the combinators are worth it.

I would argue that `.map_err()` is useful as it compliments the `?` operator. Hopefully with (and often without) `try!` blocks many of the other ones can go away. In particular I think that language constructs are almost always better than `.and_then()`.

Re:Go Replaces Interface{} with 'Any' by andrewjf on Tuesday December 14, 2021 @07:00PM (#29559942)
I would love to use this style but I find myself reverting back to the match syntax.

    match may_fail_who_knows() {
      Ok(success) => {
        do_something_with_success(success)?
      },
      Err(failed) => {
        some_error_processing(failed)
      }
    }
As `do_something_with_success` in a closure can't early return from the function (since it's in a closure), which makes sense, but just annoying to read nested results.
Re:Go Replaces Interface{} with 'Any' by stouset on Tuesday December 14, 2021 @06:28PM (#29559598)
And

    let result = may_fail_who_knows()?
      .use_value_which_might_also_fail()?
      .use_a_different_way_that_might_fail()?;
Re:Go Replaces Interface{} with 'Any' by rp1 on Tuesday December 14, 2021 @06:27PM (#29559585)
That does clean stuff up a bit, but still requires nesting if you need to access more than one value generated in the chain at a time. Go's pattern of val, err := ... is a little cleaner in that regard, but does have a lot of redundant if err != nil checks.
Re:Go Replaces Interface{} with 'Any' by BatteryMountain on Wednesday December 15, 2021 @03:51AM (#29563496)
Can you kindly post a snippet of what you want the early return on error syntax sugar to look like? I'm trying to map it in my mind on how I'd do it in C# and how it would look in Go.
Re:Go Replaces Interface{} with 'Any' by nvarsj on Tuesday December 14, 2021 @05:40PM (#29559113)
Yes, please, the iferr pattern drives me nuts. While I built a few years of my career on golang, I'm absolutely sick of the language at this point. At least we finally get generics.
Re:Go Replaces Interface{} with 'Any' by politician on Tuesday December 14, 2021 @09:18PM (#29561245)
Writing code for the happy path is 1/100th of programming. Maybe less.
Re:Go Replaces Interface{} with 'Any' by lanstin on Tuesday December 14, 2021 @10:41PM (#29561815)
There is a reason very robust software is written in C: no exceptions means you think about normal errors all the time and reason about what the difference bwtween an error openjng the file and an wrror writing the file. Sqlite, linux, cpython, all bullet proof and all C. The lack of exceptions more than makes up for memory unsafety issues.
Re:Go Replaces Interface{} with 'Any' by pjmlp on Wednesday December 15, 2021 @07:30AM (#29565081)
Re:Go Replaces Interface{} with 'Any' by Cthulhu_ on Wednesday December 15, 2021 @04:51AM (#29563849)
What alternative do you propose?
Re:Go Replaces Interface{} with 'Any' by nvarsj on Wednesday December 15, 2021 @05:06AM (#29563930)
There's many options, just look at other modern languages. The simplest are exceptions/errors that can be thrown - like you get in Java, python, typescript, etc. For something more like a traditional return value, you have std::result in rust, and error union types in zig, with language support to guarantee you check and handle the result. In C, you have macros and goto that can be used to reduce boilerplate and implement whatever pattern you want.
Re:Go Replaces Interface{} with 'Any' by kubb on Tuesday December 14, 2021 @06:52PM (#29559848)
I would also like discriminated unions, and a more sane approach to code generation than go generate and writing your own binary that parses source and spits out source code.
Re:Go Replaces Interface{} with 'Any' by rp1 on Tuesday December 14, 2021 @06:56PM (#29559891)
The Go code parsing libraries are quite good though. Not sure what else you could want re. code generation.
Re:Go Replaces Interface{} with 'Any' by kubb on Tuesday December 14, 2021 @08:03PM (#29560600)
Basically macros, like in Rust, not like in C. Rust can do stuff like the serde serialization library while Go has to rely on reflection with its obvious performance drawbacks.
Re:Go Replaces Interface{} with 'Any' by rp1 on Tuesday December 14, 2021 @08:34PM (#29560905)
Go could do something similar if you're willing to run `go generate` as part of your build process. For most Go applications, the reflection overhead is a fine price to pay for convenience, just like GC is a fine price to pay for not having to deal with the borrow checker. Obviously, these tradeoffs don't hold for all programs, but Go has definitely found a niche.
Re:Go Replaces Interface{} with 'Any' by kubb on Wednesday December 15, 2021 @05:15AM (#29563984)
I'm not sure what you're arguing with. I know these things. I'm still missing the ability to have efficient serialization (as one example) easily. It matters.
Re:Go Replaces Interface{} with 'Any' by rp1 on Thursday December 16, 2021 @05:41PM (#29584984)
I’m not arguing. I was just highlighting that you could easily implement a json library that uses go generate instead of reflection. I was positing that such a library hadn’t been made yet because the perf hit of reflection is fine for most people. Also, just an aside, if perf is a concern than JSON isn’t a good choice to begin with.
Re:Go Replaces Interface{} with 'Any' by wejick on Tuesday December 14, 2021 @07:03PM (#29559976)
what's not sane about go generate? could you elaborate
Re:Go Replaces Interface{} with 'Any' by Cthulhu_ on Wednesday December 15, 2021 @04:56AM (#29563875)
> all we need is sugar syntax for early return on error -- like more modern languages such as Rust and Zig have -- and Go may finally be pleasant to program in!

Can you provide an example? What's wrong with `return err`? That's a very explicit early return on error, no magic or language features (= added complexity) needed.