Re:Errors are values
by falcolas on Monday January 12, 2015 @10:41PM
(#8878004)
A generic maybe/result/either? Yes. However the maybe pattern is built into the NullString, NullFloat64, and NullInt64 types in the database/sql package.
[ Parent
| on HN ]
I guess that's the Go way, copy/paste the same code and change the types to be String, Float64, Int64, .... I'm not sure whether to laugh or cry.
That doesn't quite work, as the maybe pattern also implies a chaining operation (flatMap), which is impossible to implement without generics
You can still chain, you just have to do type conversion to and from the interface{} type.
It's not pretty (and not in broad adoption), but neither is it impossible.
That defeats the point of having a type system (casting is as error prone as forgetting to check errors).
Also it doesn't allow for the use of already defined functions to do the transformations, (e.g. `maybe.flatMap(existingFn).flatMap(otherFn)` wont ever work, new closures will have to be defined that do casts before calling the existing functions).
So basically, its just-as-error-prone and just-as-tedious as checking error codes but in a different way. Both benefits of the pattern are gone. Given this, I don't think that the word "impossible" is a stretch at all (not having any of the benefits of the pattern = not implementing the pattern)