|
|
|
|
|
by rbehrends
3218 days ago
|
|
> No it is not inheritance, and Go doesn't have automated delegation, it has type embedding. Different names for the same thing. > func acceptA(a A}{} // you can't pass B here This says that the function isn't polymorphic in its argument, not that the types aren't polymorphic. Function resolution that is not polymorphic is not limited to Go, but occurs in inheritance-based languages, too. Example in OCaml: class a = object method foo = 0 end
class b = object inherit a method bar = 1 end
let f (x: a) = ()
let () = f (new b)
You will get an error that the type of `new b` (= `b`) is not compatible with `a`, because they're not identical, even though `b` is a subclass of `a`.If you replace the declaration of `f` with: let f (x: #a) = ()
it'll work, because `#a` denotes a polymorphic type, matching `a` or any subclass of `a` (same as though you'd specify an interface in Go [1]). You can also cast the type explicitly to `a` to work around the error.[1] Like Go, OCaml uses structural subtyping. |
|
No, different names for completely different concepts.
> This says that the function isn't polymorphic in its argument, not that the types aren't polymorphic. Function resolution that is not polymorphic is not limited to Go, but occurs in inheritance-based languages, too. Example in OCaml:
Struct types in Go are not polymorphic in anyway period, the only way to achieve polymorphism in Go is through interfaces which are not concrete types, unlike classes.
> [1] Like Go, OCaml uses structural subtyping.
No it doesn't. There is no subtyping in Go. There is only type conversion and type assertion.
Whatever you wrote with OCaml is completely irrelevant to the discussion as the type systems are fundamentally different. but let's pretend it is.
It's interesting that you didn't bother try writing the equivalent of `let f (x: #a) = ()` in Go, because you CANNOT. An interface IS NOT a substitute for OCaml inheritance, as the later is more precise and specialized.
So no, Go doesn't support inheritance at its core. That's a false assertion. Go interfaces do not give a damn about what the actual implementation is, unlike OCaml sub classes.