3 ms·
Show HN: Goptional, an Option library for Go I made in half an hour
Hey HN,
With the release of Go 1.18, adding generics, I thought it might be fun to make a tiny Options library to bring some of that Rustacean Panicking magic into Go.
I'd love any feedback or contributions to make it, well, _less_ crappy!
Cheers.
- linkdd 5y agoThe main point of the Option type (and Result) is not because it panics when you call `unwrap`. It's about all the operations you can chain with it: - and_then - ok_or_else - map - ... This allows you to chain computations and "defer" the error handling at the end, without requiring exceptions/goto-jumps. Your implementation, and the 10 others I've seen since the release, has strictly no advantage over using `interface{}` with a `nil` check. Go 1.18 cannot have an Option type or a Result type because its generics are painfully too restrictive: - no switch on a generic type - method must have no type parameter - union types cannot be used for anything else than a type constraint - probably other things I did not notice yet Clearly, the language designers don't want you to use functional abstractions like this, for some reason.