3 ms·
I really wish Go would pursue sum types instead of generics
by ccktlmazeltov 6y ago
I really wish Go would pursue sum types instead of generics
- mrath 6y agoSum types without generics will still be good enough? I think for things like Option/Result we need both sum types and generics.
- ccktlmazeltov 6y agoI don't think you would need user generics for this to work.
- masklinn 6y agoNon-generic sum types are useful but you can’t build an option or result type out of them, so the usefulness is somewhat hampered.
- ccktlmazeltov 6y agoBut you don't need to, generic Option and Result types could be built as part of the standard library.
- masklinn 6y agoYes but then you need neither sum types nor user-defined generics, you can just build-in whatever sugar is useful or necessary (possibly repurposing existing sugar e.g. the `select` statement).
- ccktlmazeltov 6y agosum types are still super useful imo without generics.
- OceanSpray 6y agoWithout generics, sum types would have to be a built-in like maps and arrays. With generics, discriminated unions can take on all sorts of user-defined shapes.
- kccqzy 6y agoWithout generics, structs can take on all sorts of user-defined shapes. It's just that the types of its fields are fixed at declaration. Without generics but with sum types, the sum types can still take on all sorts of user-defined shapes. It's just that the types of its various alternatives are fixed at declaration.
- qtplatypus 6y agoSum types and Generics solve different but related problems. So they are not really comparable. I would like to see both get implemented.
- ccktlmazeltov 6y agoI'd say it's really hard to abuse sum types, I wouldn't say the same of generics.
- qtplatypus 6y agoThat is true. However sum types don't allow us to abstract over common code. For example a sum type wouldn't allow us to create a type safe channel "fan in" function that works for all channel types.
- shp0ngle 6y agoAren't the new interfaces from the draft, where you can list bunch of possible types, sum types? edit: oh. no. https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#type-lists-in-interface-types https://go.googlesource.com/proposal/+/refs/heads/master/des...