6 ms·
Sum types: https://go.dev/play/p/R7Bhy2LDmde https://go.dev/play/p/R7Bhy2LDmde Enums: https://go.dev/play/p/MnbmE0B3sSE https://go.dev/play/p/MnbmE0B3sSE Here
by Splizard 4y ago
Sum types:
https://go.dev/play/p/R7Bhy2LDmde https://go.dev/play/p/R7Bhy2LDmde
Enums:
https://go.dev/play/p/MnbmE0B3sSE https://go.dev/play/p/MnbmE0B3sSE
Here you go.
- esprehn 4y agoThat code appears to be using reflection to build up runtime tables? https://github.com/qlova/tech/blob/c6379c9c32e5b7b2973bc02ba935765a4aefa514/sum/sum.go#L88 https://github.com/qlova/tech/blob/c6379c9c32e5b7b2973bc02ba... https://github.com/qlova/tech/blob/c6379c9c32e5b7b2973bc02ba935765a4aefa514/sum/sum.go#L177 https://github.com/qlova/tech/blob/c6379c9c32e5b7b2973bc02ba... That's a big difference from other languages where this is all handled at compile time.
- suremarc 4y agoThis is cool, but it doesn't seem like Switch is implemented for Int, and the use of runtime reflection limits the contexts in which this implementation can be used, compared to a language construct that gets compiled into efficient machine code. I wouldn't want this to be endemic in my codebase or my dependencies.
- Splizard 4y agoEnum supports an exhaustive Switch as well, here's an example based on the blog post: https://go.dev/play/p/Ef78vLyw33g https://go.dev/play/p/Ef78vLyw33g
- arriu 4y agoIs it just me or is the syntax on these worse than the issues it’s solving?
- masklinn 4y agoIt's not you, this is a worse version of std::variant, which isn't exactly great in the first place.