3 ms·
"then breaks when passed a bundle of named functions that does not conform to that relationship." In that case, the fault lies with the thing that put unrelate
by jerf 7y ago
"then breaks when passed a bundle of named functions that does not conform to that relationship."
In that case, the fault lies with the thing that put unrelated functions together and passed them to a thing expecting them to be related. I'm not claiming that structural typing will somehow prevent programmers from deliberately writing wrong things, because I mean, what type system can make that promise? My point is that it is in my experience very rare for something expecting "something that can write bytes" to be accidentally passed "something that can write novels" or something equally unrelated, and then the world blows up in a bad way (that is, not just an exception thrown, because that's just a risk of dynamic languages, but actual bad things happening).
Dynamic languages can at least still get what I said wrong; in Go it's even harder because as others have pointed out, Write([]byte) and Write(NovelInput) Novel still can't cross. But even in dynamic languages, this isn't a problem I had.
- lmm 7y agoNothing 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.
- throwaway894345 7y agoLike 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.
- lmm 7y agoReplied elsewhere.