Hacker News new | past | comments | ask | show | jobs | submit login
What's New in Go 1.22: Cmp.or (carlana.net)
22 points by todsacerdoti 10 months ago | hide | past | favorite | 8 comments



I love how simple some things are in Go. Sure, this code does not do lazy evaluation but for a lot of situations it's good enough.

And look how short this code is thanks to languages sane defaults. In Rust you would need T : Default + PartislEQ, and all that jazz


"Note that because cmp.Or cannot do short-circuit evaluation, this will compare the product name and price of each item, even if the customer name is different, which makes doing so redundant."

Is the problem really the lack of short-circuit evaluation, or the fact that passing arguments to the function requires evaluating all arguments first? I.e. is it even possible to write a function which wouldn't evaluate all args before calling?


I loosely read it as both: Using cmp.Or does not short-circuit evaluation.

The other bit was also interesting:

> According to the proposal, the built in constant `zero` would be available in generic functions for return or comparison. However, after the proposal was accepted, it was retracted due to continued community push back against the idea of having both `nil` and `zero` in the language.

So instead of having a `zero` we have many zeroes `nil`, 0, `""`, etc. Go really eschews abstractions.


Lots of zeros is good.

One big problem with null pointers is that there's only one of them. (One of a given type doesn't help.)

Hoare's "NULL was my biggest mistake" rant proposed the wrong solution, namely no null.


I think it would be better if Go had nilptr for pointer types and "nil" only applied to interface types. That would fix the problem of "why did I return nilptr but the value != nil?" But Go should also have a named zero value that applies everywhere, or at least an operator like ? that compares anything to zero.


Presumably cmp.Or is short enough that it will usually be inlined by the compiler, at which point any side-effect-free calculations can be short circuited. I don't know in what circumstances cgo actually will perform both of those optimizations.


Here is the Godbolt. My assembly is weak, but AFAICT, it does not shortcircuit. https://godbolt.org/z/4z4Tr9Gjv


In a non-lazily evaluated language (i.e. most languages not named Haskell) it would have to be done by passing in functions ("thunks")




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: