T0
and T1
are different because they are named types with distinct declarations" (which I don't think is an satisfying explanation).Even if a B and a C "can be treated as" A's (in your example, because
both B and C are interfaces embedding A), []B and []C are not []As;
this is (I think) a general result, ie, not restricted to Go.
And here's why: you can put any A into a []A, but you can't put any
A into a []B, because the elements of B MUST be Bs.
Allowing a []B to be treated as an []A would mean allowing smuggling
a non-B into a []B by treating that []B as an []A and then putting a C
(which is an A but not a B) into it.
That would either break static type checking (Go's choice: a []B is not
a []A) or require run-time type checks when assigning into an array
element (Java's choice, but the sort of implicit performance hit that
is very unGo.)
Chris
--
Chris "allusive" Dollin
Allowing a []B to be treated as an []A would mean allowing smuggling
a non-B into a []B by treating that []B as an []A and then putting a C
(which is an A but not a B) into it.