3 ms·
There's nothing similar about Java's and Go's interfaces, except the name. Java interfaces must be implemented explicitly (they're nominal). Go interfaces are
by tomp 3y ago
There's nothing similar about Java's and Go's interfaces, except the name.
Java interfaces must be implemented explicitly (they're nominal).
Go interfaces are automatically satisfied by any type that declares matching methods (similar to protocols in other languages) (they're structural).
- twic 3y agoThey are certainly different, but to say there is nothing similar is plainly untrue - apart from nominal vs structural, they are pretty much the same. I'm not sure how significant the nominal vs structural distinction even is. In Go, a struct can implement an interface without declaring it, but the programmer still needs to deliberately write the struct to conform to the definition of the interface, so they're still coupled, just not explicitly [1]. Yes, it is possible to define a new interface which fits existing structs which weren't designed for it - but how common is that? That is, how common is it for two or more structs to have a meaningful overlapping set of methods without being designed to conform to some pre-existing interface? [1] which is obviously a bad thing
- dwattttt 3y agoGo's nominal interface feature is definitely interesting, but that section doesn't talk about how amazing it was to implement interfaces implicitly: > That notion quickly grew into the idea that value types had behaviors, defined as methods, and that sets of methods could provide interfaces that functions could operate on. Go's interfaces arose pretty much right away. If you replace Go with Java, this would've accurately described Java ~30 years ago.
- marwis 3y agoScala had it 2 years before golang, OCaml more than decade. https://en.wikipedia.org/wiki/Structural_type_system https://en.wikipedia.org/wiki/Structural_type_system
- Izkata 3y agoSo they're like python's Abstract Base Classes? (No idea which came first)