3 ms·
I'd argue optionals are just a variation of Go's interfaces (e.g. io.Reader versus io.ReadCloser)... and enums are basically typed constants in Go (you just don
by pbnjay 11y ago
I'd argue optionals are just a variation of Go's interfaces (e.g. io.Reader versus io.ReadCloser)... and enums are basically typed constants in Go (you just don't see the name "enum" anywhere).
- ntonozzi 11y agoOptionals are a way of returning either result or an error, and are not similar to interfaces. Go works around this by providing multiple return values, but because it lacks generics, it is very common to manually check for errors. I'm not sure about Swift, but in some languages you can statically check case statements to make sure that every enum has a case, which is not possible with Go.
- francescu 11y agoEnum in Swift are very powerful. They can have associated values, pattern matching, implement interfaces (conform to protocol), have functions etc. Actually it is my favorite Swift feature.
- TheHydroImpulse 11y agoOptionals can't be expressed safely in Go. They have a type of `Option<T>` or `Maybe<T>`. Swift just has this notion built as an operator. Traditional enums aren't particularly interesting. What languages like Rust and Swift have are tagged unions. They let you associate data with each variant. Moreover, pattern matching is needed to use tagged unions effectively. Constants are not "basically" tagged unions or even enums. They are not grouped into a parent type. For example, if you have a `Foo` enum with `A` and `B` variants; while matching or accepting this, you can accept a `Foo` which is guaranteed to be either `A` or `B`. How would Go typed constants achieve this?
- niccaluim 11y agoIt's impossible to implement optionals without parameterized types, which Go does not have except for some built-in types. And optionals without language support for generic functions are about as useful as an exploding hammer.