You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know there has been discussion about the visitor pattern in the Go target and that many of the problems have been fixed. But I believe problems still remain, that make it pretty awkward to use.
Here's a method of a visitor I implemented:
func (v *ParserVisitor) VisitProcedureBody(ctx *gen.ProcedureBodyContext) interface{} {
body := &ast.ProcedureBody{}
switch n := v.VisitStatementBlock(ctx.StatementBlock().(*gen.StatementBlockContext)).(type) {
case error:
return n
case *ast.StatementBlock:
body.Type = n.Type
body.StatementBlock = n
}
return body
}
The way these methods are generated is still very Java-like. Here are the problems.
The ctx methods for getting a child context like StatementBlock return an interface. So in the above example, ctx.StatementBlock() return IStatementBlockContext. This is not idiomatic Go. Unlike Java we do not create interfaces for all structs, and a general rule of thumb is that return values should be concrete types. So I don't really understand why interfaces are created for each context type.
Related to the first is the fact that the VisitX methods take concrete types, not the corresponding interface type. So this means that my code is full of type casts. At the very least if we insist on returning child context interfaces, then interfaces should should also be used in the VisitX methods.
Each VisitX method should return an error also, not just an interface{}. Go does not have exceptions, rather methods should return errors.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I know there has been discussion about the visitor pattern in the Go target and that many of the problems have been fixed. But I believe problems still remain, that make it pretty awkward to use.
Here's a method of a visitor I implemented:
The way these methods are generated is still very Java-like. Here are the problems.
Beta Was this translation helpful? Give feedback.
All reactions