Conditional Assignment Operations in engine.Eval #815
-
I'm working on a PowerFX implementation for handling relatively straightforward business logic units. I expect users to need to perform assignment (Set) operations primarily and to perform them conditionally. I've reviewed the samples closely and I've played around with the RecalcEngine a bit. So far, I am a bit surprised by how challenging this objective seems to be. The RecalcEngine can't handle Set inside a conditional expression (it says Set is an invalid Function) and the samples seem to bear this out, with the REPL sample openly assuming that Set can only be evaluated if it is the opening of a REPL command (The regex match begins with @"^\s*Set). So, from what I see so far, what I need to do is Regex match every Set operation and substitute them with some Output value like Set1, Set2, etc. Then eval the expression and match my Output back to the Set operation I swapped out, then run that Set operation. Something kinda like the below: But that doesn't seem right to me. In the Canvas App editor, there is no problem nesting an If inside a Set inside an If, so I feel like I must be missing something here, I just can't seem to find it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Power Fx requires explicitly enabling because hosts generally want control of what the expressions can do. A lot of hosts want to explicitly constrain to be trivial "side-effect" free expressions. |
Beta Was this translation helpful? Give feedback.
-
OK, Awesome! Yes, this is getting me MUCH further along - thank you! So let me run this by you and see if I'm getting it right here. If we look at the REPL example then there is a simple code block there for recognizing Set() and updating a var without using Eval because the example does not set the EnableSetFunction flag...
This basic approach is just so people can see it is possible, but a more mature approach has to set the EnableSetFunction flag, then inspect each expr as it comes through to search for Set functions, then check to make sure the ident they are declaring already exists (can't Set an ident that hasn't already been declared). So, then, something more functional would look kinda like this:
Feels like I'm getting there now... |
Beta Was this translation helpful? Give feedback.
Power Fx requires explicitly enabling because hosts generally want control of what the expressions can do. A lot of hosts want to explicitly constrain to be trivial "side-effect" free expressions.
Please take a look at the following test cases on how to enable Set function and how Power Fx engine deals with mutation functions.
https://github.com/microsoft/Power-Fx/blob/main/src/tests/Microsoft.PowerFx.Interpreter.Tests/SetFunctionTests.cs