9 ms·
As someone quite new to Go, who has found a lot to like, I find the lack of a built in way to model optionality (besides pointers, which rather muddy the intent
by deergomoo 3y ago
As someone quite new to Go, who has found a lot to like, I find the lack of a built in way to model optionality (besides pointers, which rather muddy the intent) absolutely baffling.
I want to use values over pointers for as much as I possibly can; the sort of stuff I work on is not so performance sensitive that it would be a problem. But zero-values are totally insufficient for modelling optional values, especially for numeric data.
Now it has generics I could implement my own Optional[T] type, but backup from the compiler forcing me to check a value exists before doing anything with it is what really makes optional types shine in languages that have it.
- Mawr 3y agoYou can just do it yourself as you mention and the compiler does force you to check: https://gist.github.com/MawrBF2/0a60da26f66b82ee87b98b03336e1f84 https://gist.github.com/MawrBF2/0a60da26f66b82ee87b98b03336e...
- deergomoo 3y agoThat's cool, thanks for the link! Hadn't considered that multiple return values could cover this case