Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operating overloading does not work with functions defined using expr.Function #362

Closed
purplefox opened this issue Apr 25, 2023 · 3 comments · Fixed by #408
Closed

Operating overloading does not work with functions defined using expr.Function #362

purplefox opened this issue Apr 25, 2023 · 3 comments · Fixed by #408
Labels

Comments

@purplefox
Copy link

If you attempt to overload an operator and the implementation of the function is defined using expr.Function as opposed to having the function in the env, then it fails to compile.

Please see #360 for more details

@antonmedv antonmedv added the bug label Aug 12, 2023
@antonmedv
Copy link
Member

Here is the test that 🔴fails and needs to be fixed.

func TestOperator_Function(t *testing.T) {
	env := map[string]interface{}{
		"foo": Value{1},
		"bar": Value{2},
	}

	program, err := expr.Compile(
		`foo + bar`,
		expr.Env(env),
		expr.Operator("+", "Add"),
		expr.Function("Add", func(args ...interface{}) (interface{}, error) {
			return args[0].(Value).Int + args[1].(Value).Int, nil
		}),
	)
	require.NoError(t, err)

	output, err := expr.Run(program, env)
	require.NoError(t, err)
	require.Equal(t, 3, output)
}

type Value struct {
	Int int
}

Any volunteers? Tests should be added in https://github.com/antonmedv/expr/blob/master/test/operator/operator_test.go

@nikolaymatrosov
Copy link
Contributor

I tried to implement functionality that passes the test in #408. It would be great if you can review the PR.

@Q16G
Copy link

Q16G commented Feb 14, 2024

Recently I was also troubled by this issue, I saw the issue, but I found that the new version does not seem to solve the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants