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.