9 ms·
Unfortunately, its impossible to implement Maybe / Result / Either in Go. Doing that requires generics.
by spion 12y ago
Unfortunately, its impossible to implement Maybe / Result / Either in Go. Doing that requires generics.
- falcolas 12y agoA generic maybe/result/either? Yes. However the maybe pattern is built into the NullString, NullFloat64, and NullInt64 types in the database/sql package.
- spion 12y agoThat doesn't quite work, as the maybe pattern also implies a chaining operation (flatMap), which is impossible to implement without generics
- falcolas 12y agoYou 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.
- spion 12y agoThat 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)
- lmm 12y agoI 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.