by pcwalton · 11y ago · 0 comments
Then make a Draw() method on the single object default to what you expect. The simple inline explanation is:
type SingleObject struct {
Cowboy
Shape
}
Access is as follows to be unambiguous: Woody.Cowboy.Draw()
Woody.Shape.Draw()They all are the same interface and can be passed into each other's methods, leading to a logical error.
Yes, sometimes this isn't an option when you want to satisfy the interface for two different libraries, but I've not seen this occur very often. More likely you're in control of one of the interfaces; so rename your method to be more descriptive.
This is assumed in the description of the problem. Go has no solution for it. (Java doesn't either, but this is one of the things that C# got right over Java; it's unfortunate that Go has the same issue.)
> , but I've not seen this occur very often.
Sure, it's a problem in theory. Whether it's a problem in practice is a different question. I'd be interested to see examples of this problem in the wild.
http://stackoverflow.com/questions/2598009/method-name-colli...
http://stackoverflow.com/questions/4033329/multiple-interfac...
http://stackoverflow.com/questions/11343238/java-implementin...
http://stackoverflow.com/questions/17484325/implementing-mul...
http://stackoverflow.com/questions/9863835/two-interfaces-wi...
http://stackoverflow.com/questions/19959371/calling-same-met...
http://stackoverflow.com/questions/14290902/multiple-interfa...
etc. etc. I got bored after the first page of Google results :)
I implemented a DB driver that had its own Next() method. Later I wanted to implement the SQL package interface. That interface contains Next() (http://golang.org/src/database/sql/sql.go?s=40568:40595#L157...).
Choices - break backwards compatibility and change the interface I control or convolute the type definitions to create a type specific to the database/sql interface.
This limitation in combination with methods only being distinguished by name, not by parameters, turns out to be very annoying -- especially as interfaces profliferate.
https://play.golang.org/p/t0gjJHjU7y
Edit: another variation on the same idea: https://play.golang.org/p/FqSFpMKQuv