3 ms·
There's a way to make channels easy to use and abstract. Just make a typecast function. Details in a comment on the original post. The idea is that you create
by tonyhb 12y ago
There's a way to make channels easy to use and abstract. Just make a typecast function. Details in a comment on the original post.
The idea is that you create a library which communicates using channel interface. Great. Now you need to add `i.(myType)` wherever. So, create a function that accepts an interface, switches on type (switch i.(type)) and returns a value with your concrete type.
It's a 6-liner solution to most of this rant about channels.
- djur 12y agoIn a statically typed language, it shouldn't be necessary to break type safety to implement basic abstractions. The only thing interface{} provides over void* is safe typecasting.
- frou_dh 12y agoWell that's not to be sneezed at, even though it remains crude.
- tonyhb 12y agoIn other statically typed languages you don't break type safety to implement abstractions. In go, where interfaces are implemented by default and `interface{}` covers everything, you also don't need to. It's just a little bit (~6 lines) of code to add to guarantee it while they figure out a nice way to add generics.