86 points · by katcipis · 10y ago · 51 comments
A man orders coffee in a restaurant.
"Would you like it with or without cream?"
"Without"
A few minutes later the waiter returns.
"I'm sorry sir, we've run out of cream. Would you like it without milk instead?"
Just because a language supports exceptions does not mean you always have to use them. You can always use a helper function to catch any error and return it as a value.
You could argue that Go can use Exceptions in the same manner as X lang too, by using panic and recover... but most people seem to not like that.
With Go and Rust, return value error handling is just simply nice. There's no question about if a function might error out, just look at the signature.
This would not be "replicating" anything, exceptions already are values in most languages. The only thing to do is wrap your call with a function, like "err = catch-all(expr)".
> ou could argue that Go can use Exceptions in the same manner as X lang too, by using panic and recover...
Except that it would be cumbersome ;-) because panic/recover works only in combination with defer[0].
> With Go and Rust, return value error handling is just simply nice.
Using non-exceptional control flow for errors can be useful depending on the circumstances. Using exceptions is simply nice in many other cases.
> There's no question about if a function might error out, just look at the signature.
Just look at the documentation. Code defensively.
err = catch-all(expr)
If your language does not provide a similar tool, you can't just use a function because "expr" is evaluated before the function is called. I had Lisp macros in mind when writing this. A poor man's approach that is still generic is to wrap the expression in a closure: err = catch-all(() -> expr)
Not too cumbersome, but YMMV.When a nil value is converted to an interface, it carries type information. When an interface carries type information, it isn't nil.
[1] github.com/joeshaw/multierror
var m map[T]T
you're basically writing var m *runtime.hmap
which is a nil pointer. Go has sane zero values, so it makes sense that reading from a nil map returns the zero value for whatever type its values are.However, there's a trade-off: either Go's maps allow nil assignment (in a similar fashion to slice's `append') _or_ it retains its "reference" (forgive me for using that term) semantics.
Go's designers decided that maps should differ from slices in that regard, and I tend to agree. I think it'd be a mistake to have to always return a map _just_ in it's newly allocated.
Why on Earth should a user of a high-level language have to care about language implementation details?
> What the actual fuck ???
Exactly.
Okay, but what was the rationale for designing the language like that?
func (o *Object) ReturnSomething() int {
if o == nil {
return 0
}
return o.SomeOtherValue
}
It is also therefore legal to return in an interface value a nil pointer to a struct of a particular kind, and therefore "an interface containing a nil pointer of a particular type" and "an interface containing nothing at all" are fundamentally different things that can not be collapsed together.So as Vendan says, it is just part of the language. All languages have this sort of wart in them, where two or more perfectly sensible decisions interact to create something that isn't sensible at first glance.
(I'm still in favor of going back in time and changing Go to have non-nillable values, but that ship has certainly sailed.)
Also, sometimes nil is a perfectly valid value of something and can implement interfaces. You can call methods on nil receivers because method calls are just curried function applications over the value. It would be odd if the interface compared equal to nil just because I happened to implement it with a value that's valid with nil.
In this case, it's an implicit conversion from "nil" to the "nil value" of a given type. This is convenient in many cases, but causes confusion in other cases.