4 ms·
The way Rob is describing this reminds me of Haskell's Typeclasses, of which Go's interfaces are basically an ad-hoc informal version (sans default implementati
by eridius 15y ago
The way Rob is describing this reminds me of Haskell's Typeclasses, of which Go's interfaces are basically an ad-hoc informal version (sans default implementations) without parametric polymorphism (but that's another topic entirely).
- quatrevingts 15y agoThey're similar in that they both allow a type to be associated with a supertype post-declaration, which is unusual in an OO language; they differ in that Go uses structural subtyping rather than nominal subtyping which is much more prevalent.
- eridius 15y agoI had not heard the terms "structural subtyping" and "nominal subtyping" before, so thank you for that (Wikipedia was able to define them). And yes, I realize that Go's interfaces are implicit whereas Haskell's typeclasses are explicit. I was thinking more along the lines that Haskell's typeclasses provide the exact same type of functionality that Rob is talking about wit Go's Interfaces, e.g. a type Foo can be a TopologicalGenus and a GrassGenus and a Square and an Area and a Symmetry all at the same time, by declaring the appropriate typeclass interfaces. They're also somewhat similar from a usage standpoint, where in both Haskell and Go I can take a type Foo and declare methods on it that conform to a known type Bar (e.g. declaring an interface Bar Foo in Haskell, or simply declaring the methods from Bar in Go) and now my type Foo can be treated as that type Bar even if I don't control the source to Foo.
- masklinn 15y ago> Haskell's Typeclasses, of which Go's interfaces are basically an ad-hoc informal version Uh no they're not. Go's interfaces are just named structural supertypes.