On Dec 6, 5:01 pm, Russ Cox <
r...@golang.org> wrote:
> > Shouldn't compiler allows this substitution?
>
> Even if the compiler allowed it, the program wouldn't run correctly.
http://research.swtch.com/2009/12/go-data-structures-interfaces.html
> describes the data structures involved here.
> The values returned by your two GetA methods have
> different memory layouts. That can't work.
> To make it work like in Java, Go would have
> to adopt Java's object model (everything is a pointer,
> dynamic method dispatch at the call sites, no itables),
> which is a pretty fundamental change and unlikely
> to happen.
It wouldn't work in Java either, except that the compiler generates
hidden adapter methods in the subclass to do the type conversion. It
seems like a roughly analogous approach might work in Go: at the point
where one interface is being cast to another one, the compiler could
check whether there are some methods that have convertible return
types. If so it could generate a new interface value with conversion
functions substituted in the appropriate slots.
- Brian