On Mon, Nov 14, 2011 at 8:23 PM,
6355...@qq.com <6355...@qq.com> wrote:
I want to know the underline reason why the language doesn't allow
pointer to pointer and pointer to interface as a anonymous field of
struct
The reason why you can't have pointer to pointer and pointer to interface anonymous fields is that these types don't have methods. The whole point of anonymous fields is that methods get promoted. I already explained why interfaces don't have methods: a lot of people were using pointers to interfaces incorrectly and unnecessarily, and there weren't any known valid uses, so the language was changed to actively discourage this usage by making pointers to interfaces have no methods.
As for pointers to pointers, they don't have methods because there isn't really a reason to implement it, and there's some uneasiness about what methods you can call where. Such as, by dereferencing a `type P *T`, you might suddenly be able to call *T methods and not P methods on it. While arguably its possible to sort this out, the seeming uselessness and confusion were enough for the language authors to disallow it.
You can get around both of these restrictions by having the pointer or interface be an anonymous field of a struct, and then have a pointer to that struct be an anonymous field.