Switch on arbitrary types, followed by typecasting? That's the go way. No surprises. Explicit instead if implicit behavior.
Switch on arbitrary types, followed by typecasting? That's the go way. No surprises. Explicit instead if implicit behavior.
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 ).
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.Which is exactly what pattern matching does, which is what I was responding about.
Edit ( since i can't reply) : just look at https://developer.apple.com/library/content/documentation/Sw... with something like the barcode example.
It's all compile time checked.
Sooo just accessing fields on a struct? I don't understand the distinction you are trying to make.
The complexity is inherent and you can either choose to make it implicit and play the losing game of ensuring it doesn't fail with tests or make it explicit from the outset.
Great, now my enums can panic because I have to play "be the typechecker", instead of focusing on something actually important like business logic.
How is this not seen as universally a bad thing?