5 ms·
I see where you are coming from, but that isn't the go way. Switch on arbitrary types, followed by typecasting? That's the go way. No surprises. Explicit inste
by xyzzy_plugh 10y ago
I see where you are coming from, but that isn't the go way.
Switch on arbitrary types, followed by typecasting? That's the go way. No surprises. Explicit instead if implicit behavior.
- dbaupp 10y agoMaybe you're visualising something different, but I don't see how ADT enums are implicit. Could you explain?
- coldtea 10y agoIf anything, ADT's would make it even more explicit. And pattern matching will make it impossible to miss cases.
- rwj 10y agoNot recommending either way, but if the language supported algebraic types with pattern matching, it would still be explicit.
- bsaul 10y agoIf you have a look at swift, it's also very explicit, and non-magical ( which is why i like it so much). The difference with go is that you can't make any type error when unwrapping the enum, because the compiler knows what are the different possibilities. I see no reason for go not to adopt it, in all honesty. it's nothing like generics, because it doesn't seem to add complexity to the rest of the language ( imho ).
- brandonbloom 10y ago> you can't make any type error when unwrapping the enum This is also true in Go with type switches: switch y := x.(type) case MyStruct: And with type assertions: y, ok := x.(MyInterface) In both cases, y is of the correct type. The ok is optional, if omitted and the assertion fails, you'll get a runtime panic.
- dbaupp 10y agoy is of the type you asked for, yes, but that isn't necessarily the type that should be there if the programmer makes a mistake, or if the underlying code changes. With an enum, the types are written in one place, and the compiler connects the dots, giving compile-time errors about mistakes/mismatched expectations. This similar to how one can call `foo(x)` and the return value's type is known, no need to first cast the function to the type the programmer thinks it should be. The casting pushes type errors to execution-time rather than compile-time.
- brandonbloom 10y ago> The casting pushes type errors to execution-time rather than compile-time Which is exactly what pattern matching does, which is what I was responding about.
- bsaul 10y agoOk, i'm starting to understand what we're disagreeing on. You're talking about pattern matching with dynamic expressions ( don't know the official name for that), such as what you find in erlang, i'm talking very basic enum unwrapping based on types, which is compile time checked. Swift also adds some kind of dynamic expression to patterns, which may break exhaustiv checking, but in the case of go, just a simple type check would be a great start. Edit ( since i can't reply) : just look at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html https://developer.apple.com/library/content/documentation/Sw... with something like the barcode example. It's all compile time checked.
- brandonbloom 10y ago> very basic enum unwrapping based on types, which is compile time checked Sooo just accessing fields on a struct? I don't understand the distinction you are trying to make.
- 10y ago
- stewbrew 10y agoI don't understand the downvotes since that's about the official stance of the go team -- see the FAQ (cited somewhere else). Agree with it or not.