dsc862009 2017-07-22 17:15
浏览 40
已采纳

使用界面时处理结构字段

The title might be misleading, but to the point...

I have a single interface Expression:

type Expression interface {
    String() // skiped in implementation below
}

The interface is implemented by multiple structs, some of which implements same interface as a field value:

type IdentExpression struct {
    value string
}

type UnaryExpression struct {
    token string
    value Expression
}

func (a *UnaryExpression) Simplify() {
    var finalValue Expression
    switch a.value.(type) {
    case UnaryExpression:
        tmp := a.value.(UnaryExpression)
        switch tmp.value.(type) {
        case UnaryExpression:
            tmp = tmp.value.(UnaryExpression)
            finalValue = tmp.value
        }
    }

    a.value = finalValue
}

Given expression -(-(-(1))), UnaryExpression.Simplify() will simplify the expression to -(1). (play)

I would like to extend the interface with Simplify() method:

type Expression interface {
    Simplify()
    String() string
}

// ...

func (a IdentExpression) Simplify() {} // do nothing

Resulting code does not work (play):

main.go:29: impossible type switch case: a.value (type Expression) cannot have dynamic type UnaryExpression (missing Simplify method)

main.go:30: impossible type assertion:

UnaryExpression does not implement Expression (Simplify method has pointer receiver)

main.go:59: cannot use UnaryExpression literal (type UnaryExpression) as type Expression in field value:

UnaryExpression does not implement Expression (Simplify method has pointer receiver)

main.go:60: cannot use UnaryExpression literal (type UnaryExpression) as type Expression in field value:

UnaryExpression does not implement Expression (Simplify method has pointer receiver)

I have found this answer, which looks similar however I do not know how to apply it in my case.

  • 写回答

1条回答 默认 最新

  • duanbin3021 2017-07-22 17:39
    关注

    The key here is that you are using a pointer receiver in your definition of Simplify() with respect to UnaryExpression:

    func (a *UnaryExpression) Simplify()
    

    The other methods you are implementing don't use a pointer receiver:

    // One example
    func (a IdentExpression) Simplify() {}
    

    Typically, in Go, it is considered best practice to have all methods on the same type use the same type of receiver (i.e. if one method uses a pointer receiver, they all should. Likewise, if one method uses a non-pointer receiver, they all should for that particular type).

    In this case, the code will compile if you remove the pointer receiver from the Simplify method of UnaryExpression. Hope this helps!

    Edit: Here is a more comprehensive answer that explains exactly why this error happens, it's really a good read.

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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图