dongsha2792 2016-11-25 11:10
浏览 28
已采纳

Golang:为什么比较后指针选择器是非法的?

I'm reading the spec about selectors: https://golang.org/ref/spec#Selectors

enter image description here

Why q.M0() is invalid. While p.M0() is valid and q=p. Very strange for me.

The relevant source code:

type T0 struct {
    x int
}

func (*T0) M0()

type T1 struct {
    y int
}

func (T1) M1()

type T2 struct {
    z int
    T1
    *T0
}

func (*T2) M2()

type Q *T2

var t T2     // with t.T0 != nil
var p *T2    // with p != nil and (*p).T0 != nil
var q Q = p

p.M0()       // ((*p).T0).M0()      M0 expects *T0 receiver
q.M0()       // (*q).M0 is valid but not a field selector
  • 写回答

1条回答 默认 最新

  • douzhao2047 2016-11-25 11:42
    关注

    Why q.M0() is invalid. While p.M0() is valid and q=p. Very strange for me.

    q is initialized like var q Q = p, but it does not mean they are equal. The assignment is valid because it does not violate the assignability rules, but type of q is different than type of p.

    Type of q is Q (where type Q *T2), and type of p is *T2.

    In Go, methods belong to a specific type. When you do this:

    type Q *T2
    

    It creates a new type named Q (*T2 being its underlying type). The new type will have 0 methods, it does not "inherit" any methods from *T2, hence q.M0() will be a compile-time error:

    q.M0 undefined (type Q has no field or method M0)

    Note:

    You may still think it's strange because M0() is declared like this:

    func (*T0) M0()
    

    It has *T0 receiver so it belongs to the type *T0, yet type of p is *T2, so *T2 should not have this M0() method and therefore p.M0() should also be invalid. But T2 is a struct which embeds *T0, and so methods of *T0 are promoted, and they will be in the method set of T2.

    Also see this related question: Golang: Why selector to pointers is illegal after comparison?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试