4 ms·
I think the chances that people independently developing two separate libraries will accidentally give their methods exactly the same name, with the same types
by pcwalton 13y ago
I think the chances that people independently developing two separate libraries will accidentally give their methods exactly the same name, with the same types of their arguments in the same order and with the same return value, allowing this "emergent behavior" are very low in practice. Rather, Close() and MarshalText() are really people implementing explicit, well-known interfaces defined in the standard library. MarshalText() is a good example of this—I would have named it something like Serialize() if not for the well-known, named interface I explicitly wanted to conform to.
The primary benefit that structural interfaces give you, IMO, is less typing. Which has plenty of value, don't get me wrong; it's awesome that Go has successfully reduced the amount of syntactic overhead needed to implement interfaces. But I think the benefits of it shouldn't be overstated.
- Retric 13y agoA better example of Emergent behavior might be something that implements A and B it also incitements C if C is a subset of A and B. Or if your refactoring and decide to split an interface so what was X is now Y and Z. Consider adding unkillable NPC's to a game. Rather than having special code to handle MOB's that you can't kill you remove HP's from the MOB interface and add a killable interface and now the type system helps you refactor your code by complaining when you try to harm something without HP's. At the same time you don't have to change any existing objects.
- pcwalton 13y agoMaybe, but I'm not so sure about that either. Consider the two possibilities: 1. A and B were defined in separate libraries. In this case, the arguments about the likelihood of two types happening to define two methods with the same name and signature apply equally well to the likelihood of the two interfaces having a common subset at all. 2. A and B were defined in the same library. Here, I suspect most library authors in a language with explicit interfaces would notice this and factor out the common methods in A and B into a separate interface that A and B derive from. You might argue that the library writer could forget to do this, but I think it's not much more probable than the scenario in which A and B have no structural subset because the common functionality has a different names, or has a different return type, or has the arguments in a different order.
- Retric 13y agoConsider cases where A is defined in a library that B depends on. You might have Name and Location where Location is (latitude longitude altitude). Now you want to map things, while existing objects Trucks with Name and Location are easy you want also map stuff with a name and address. You can get a latitude and longitude from an address, but not altitude and adding meaningless altitude is IMO a bug waiting to happen.
- rdtsc 13y agoI can see you point in general about usually this not being a problem and I agree with you (I come from Python here so duck-typing is the name of the game if you wish). However I was trying to think where this might break (playing devil's advocate), I can think of specialized domains with lots of code referring to same terminology. Ok like we have Close(). One could have Commit(), or MtxDeterminant() or Launch() things like that, where just a lots of people using same terminology in a domain are just bound to create collisions (on individual names!). Now high likely it is that a combination of those would be hit, not very sure on that.
- seanmcdirmid 13y agoCowboys draw their guns; artists draw their pictures; an artist cowboy draws pictures and guns. It happens more than you think, and name hygiene is a big deal in programming languages. The problem goes away with less ambiguous naming conventions (e.g. DrawGun() and DrawPicture()), but overloading is so gosh darn convenient, and thinking of unambiguous names is quite difficult. C# is really the only mainstream language that gets this right with explicit interface implementations.
- pcwalton 13y agoYes, that's a drawback to implicit interfaces I had forgotten: it fails to solve the cowboy-shape problem (and worse, there's no way to fix it in the future without introducing a whole new kind of interface: the language seems kind of stuck with this issue).
- robert_tweed 13y agoAgreed about C# multiple interface handling. I'd really like to see something like that added to Go at some point. In fact it's probably not that far away from where Go is now. Go already has something very similar for implicit composition / mixins. Now, if you do something like this: type Foo struct { Bar } Then Foo automatically has the methods of Bar, e.g. Foo.bar() instead of Foo.Bar.bar() although the former is really just shorthand for the latter. If you then have: type Foo struct { Bar Baz } Then assuming that Baz also has the method bar() then you now need to be explicit about it and call Foo.Bar.bar() or Foo.Baz.bar() because the compiler doesn't know which one Foo.bar() means. Of course, this is slightly different to interface disambiguation, but there's no reason why you couldn't have two function definitions with the same name and some added interface qualifier - the functions would then only be callable if the object is cast to an interface. In Go, casting to an interface is a bit like boxing, as it creates a separate vtable for that object's methods to match the interface ABI. Hence, you could have say: a := Foo(foo) b := Bar(foo) Now a has a vtable entry for Foo::foo() -> a.foo() and b has a vtable entry for Bar::foo() -> b.foo(). On the other hand, there would be no foo.foo() method unless one were declared separately without any interface qualifier.
- stormbrew 13y ago
- latch 13y agoI know what you mean; it doesn't [thankfully] happen for complex interfaces, but I maintain that it's useful (OP possibly overstates) as a composition of smaller interfaces. It's completely possible to write a type which satisfies http.ResponseWriter, both technically and in spirit, unintentionally while still being useful. I've used it around a Stats interface which required a single method: Statistics() map[string]string and have been able to hook up otherwise independent code into a common logger. Speaking of loggers, the fact that the built-in log package doesn't expose an interface has always been an annoyance to me specifically because I can't build my own code and know that it'll satisfy a log.Logger interface in other projects.
- _pmf_ 13y ago> I think the chances that people independently developing two separate libraries will accidentally give their methods exactly the same name, with the same types of their arguments in the same order and with the same return value Don't forget "with the same semantics".