【发布时间】:2017-12-28 15:51:57
【问题描述】:
标题可能具有误导性,但切中要害……
我只有一个接口Expression:
type Expression interface {
String() // skiped in implementation below
}
接口由多个结构体实现,其中一些实现了与字段值相同的接口:
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
}
给定表达式-(-(-(1))),UnaryExpression.Simplify() 会将表达式简化为-(1)。 (play)
我想用Simplify()方法扩展接口:
type Expression interface {
Simplify()
String() string
}
// ...
func (a IdentExpression) Simplify() {} // do nothing
结果代码不起作用(play):
main.go:29: 不可能的类型切换案例:a.value(表达式类型)不能有动态类型 UnaryExpression(缺少 Simplify 方法)
main.go:30: 不可能的类型断言:
UnaryExpression does not implement Expression (Simplify method has pointer receiver)main.go:59:不能在字段值中使用 UnaryExpression 文字(类型 UnaryExpression)作为类型表达式:
UnaryExpression does not implement Expression (Simplify method has pointer receiver)main.go:60:不能使用 UnaryExpression 文字(类型 UnaryExpression)作为字段值中的类型表达式:
UnaryExpression does not implement Expression (Simplify method has pointer receiver)
我找到了this answer,看起来很相似,但是我不知道如何在我的情况下应用它。
【问题讨论】:
-
删除 Simplify 函数中的指针引用。您的代码应该可以工作。
标签: pointers go struct interface