Re:Is Go Duck-Typed?
by lmm on Thursday March 05, 2020 @10:01AM
(#22494024)
Nothing can go wrong with a single function like Write - but in that case you don't need a structural type, you can just pass a write function. As soon as you pass a bundle of two or more functions, you're implying a relationship between them, and it's then easy to accidentally violate that relationship - for instance, a structural type will almost always admit a mutable implementation where the relationship breaks down if the thing is mutated in between calls to the two functions.
[ Parent
| on HN ]
Re:Is Go Duck-Typed?
by throwaway894345 on Thursday March 05, 2020 @11:42AM
(#22495181)
Like I said elsewhere in the thread, you can have the same problem with callbacks:
func receiver(next func() bool, getItem func() Foo) {}
vs type FooIter interface { Next() bool; Item() Foo }
func receiver(iter FooIter) {}
You're more likely to pass the wrong set of callbacks than to pass the wrong FooIter implementation since the type system doesn't require the callbacks to be related to the same data nor named any certain way.