function Dot for . operator does not have a correct signature
Hello,
I am trying to use the operator override feature to add a custom operator (this error is thrown when trying to override an existing operator as well).
I've tried referencing both examples (Go Docs and Readme), however I am getting the following error:
function Dot for . operator does not have a correct signature
Here is my code:
func run(code string, variables map[string]interface{}) (interface{}, error) {
options := []expr.Option{
expr.Env(variables),
expr.Env(Env{}),
expr.Operator(".", "Dot"),
}
program, err := expr.Compile(code, options...) // Throws error
if err != nil {
return nil, err
}
env := Env{}
output, err := expr.Run(program, env)
if err != nil {
return nil, err
}
return nil, nil
}
type Env struct{}
func (Env) Dot() { fmt.Println("FOUND A DOT") }
Additionally, I found issue #175 which looked like they had come across the same problem and solved it, unfortunately they didn't post their solution.
Any help would be greatly appreciated. Assuming this is doable, I'm happy to submit a PR with updates to the docs for future use!
Sorry, I’m busy with other projects right now.
According to the docs, expr allows you to define new functions or override (existing) binary operators.
Specifically, there is no API to define new operators (yet). Such an API would at least need some precedence specification, which is currently all done internally.
Anyway, . is not an operator but rather a reserved word that you cannot override.
For a list of overridable operators: https://github.com/antonmedv/expr/blob/d71dab79bac52529bce7f38771d69cbeefaf1a32/parser/parser.go#L38
Yes, true. The syntax is bot overwriteable right now.
You can use ast.Patch to override dot behavior.
https://github.com/antonmedv/expr/blob/master/docs/Visitor-and-Patch.md