-
-
Notifications
You must be signed in to change notification settings - Fork 413
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
Functions defined on the struct are bidden to the Env they were compiled with #695
Comments
This is intended behaviour. You passed env.Get to Function: expr.Function("get", env.Get, new(func() any)), In Golang this makes reciver argument bidden to env. You can simplify your code and do not use expr.Function and use methods on env directly. Please, try this approach and see if it works. |
But it will be slower and I can't use type hints to get helpful error message, right? It is not the trade-off I am willing to make... |
It will be just a bit slower. Types will be inherited. |
If I understood correctly, what you trying to achieve you can try this approach: create a function which will be bidden to and env outside of expr. Use expr.Function to call it. This way you can achieve fastest calling speed and preserve bindings. I will try to write some code when I can reach my computer |
The program I've shared gives a good example of what I am trying to achieve:
Currently I am patching functions to pass var funcGetAttr = expr.Function(
"getAttr",
func (env expr.Env, args ...any) (any, error) {
fmt.Println(env.(*myenv), args[0].(string))
},
new(func(string) string),
) |
This is a nice idea. Expr already has patcher for context. We can have a patcher for env as well. |
For example, the following program can't be reused with different environments, because functions are bidden to the Env they were compiled with:
The program output:
Expected:
I can patch the code to pass
$env
as the first arg of the function, but then users get weird error messages if compilation fails.The text was updated successfully, but these errors were encountered: