|
|
|
|
|
by camus2
3224 days ago
|
|
> Go has automated delegation, and as we know, (automated) delegation IS inheritance [1, 2]. Some implementations of delegation are a bit more limited, some are a bit more expressive, but fundamentally they have the same purpose. No it is not inheritance, and Go doesn't have automated delegation, it has type embedding. Given struct A, if a function requires A, you can't pass any type B that embeds A, you must pass A. type A struct {
Foo int
}
type B struct {
A
}
func acceptA(a A}{} // you can't pass B here |
|
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:
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:
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.