4 ms·
You don’t need to implement a generic option type. It’s easy enough to implement ad hoc like we do/did with linked lists pre-generics.
by throwaway894345 4y ago
You don’t need to implement a generic option type. It’s easy enough to implement ad hoc like we do/did with linked lists pre-generics.
- kaba0 4y agoAnd then you just cast everywhere? Doesn’t sound too ergonomic.
- throwaway894345 4y agoNo, you just define it ad hoc locally. type OptionInt enum { Int(int), None } Yeah this means you would have to write that line for every type, but that’s pretty in-line with Go’s philosophy that a little bit of copying is better than a little dependency. But the people who optimize for keystrokes above all else will take issue with it.
- kaba0 4y agoThat’s probably the only amount of copying that might be okay. Anything more than that and you have n places of code living independently of each other, making refactoring/bugfixing impossible.
- throwaway894345 4y agoYes, if the code is complex, this becomes tedious. But you'd be surprised how often this sort of thing suffices. It turns out not everything needs a dozen combinator methods. See also a linked list implementation: `type List struct { Next *List, Item int }`.