and I've found something that I don't yet understand.
Why can't a pointer to a struct that embeds some type be passed
to a function taking a pointer to the embedded type as in:
package main
type A struct{}
type B struct{ A }
type C struct{ B }
func (a *A) F() {}
func (a *A) G(f func(*A)) { f(a) }
func F(*A) {}
func main() {
a := new(A)
b := new(B)
c := new(C)
a.F() // ok
b.F() // no problems
c.F() // no problems
F(a) // ok
F(b) // Error: "cannot use b (type *B) as type *A in function argument"
F(c) // Error: "cannot use c (type *C) as type *A in function argument"
F(&b.A) // explicit workaround
F(&c.A) // explicit workaround
a.G(F) // ok
b.G(F) // no problems - even though G applies F(b) !!!
c.G(F) // no problems - even though G applies F(c) !!!
}
It seems to me that a pointer to a B could always be provided where a
pointer to an A is expected,
and this IS the case for methods.
The problem is more apparent when using functions as arguments. In that
case there doesn't seem to be any workaround,
even though as I noted the applications F(b) and F(c) are ok within method
G.
Can anyone please explain this?
Thanks,
Peter.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.