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

Override operators with the scope just like functions #2631

Open
gwhitney opened this issue Jul 20, 2022 · 2 comments
Open

Override operators with the scope just like functions #2631

gwhitney opened this issue Jul 20, 2022 · 2 comments
Labels

Comments

@gwhitney
Copy link
Collaborator

gwhitney commented Jul 20, 2022

f(x,y) = (y==0) ? undefined : x%y; evaluate('mod(22,0)', {mod: f})\n evaluate('22 % 0', {mod: f})

yields [undefined, 22].

That is an interesting case. What happens right now is that functions and constants can be provided by the scope passed to math.evaluate, but this will not have effect on operators. I.e. passing mod or add or so to scope will not replace the operators % and + etc. It makes sense that they would.

Originally posted by @josdejong in #2617 (comment)

The purposeof this issue is to discuss whether overriding mod, add, etc. in the scope should affect the outcome of infix operators like %, +, etc. (And if the conclusion is so, serve as a marker that a PR to do this is needed.)

@gwhitney
Copy link
Collaborator Author

My vote is yes. My reasons:

  1. I think we should strive for the operators to be exactly syntactic sugar for the function calls (even though they are generally the preferred read/write syntax). Therefore, their behavior should be the same.
  2. Jos and I have discussed the possibility (in some issue or other, but sorry I couldn't find the reference) of collapsing FunctionNode and OperatorNode (with just a flag for whether it was written infix) into a single class. Overall, it seemed like a good idea. Obviously that will be facilitated by having the behavior be the same. :)

@josdejong
Copy link
Owner

Yes, I agree. It makes a lot of sense. And I don't think there is a use case where you would not want the operators consistent with the corresponding functions.

So, let's go for it 👍 .

@gwhitney gwhitney changed the title Should it be possible to override operators with the scope? Override operators with the scope just like functions Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants