You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Biestro (Jira)" <ji...@apache.org> on 2024/08/26 09:21:00 UTC
[jira] [Commented] (JEXL-426) Value of Captured Variable
[ https://issues.apache.org/jira/browse/JEXL-426?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17876643#comment-17876643 ]
Henri Biestro commented on JEXL-426:
------------------------------------
The current behavior- ie the capture happens at definition time - should not be altered.
However, if you want the captured variable to be a reference rather than a value, you may want to check Optional/JEXL-342.
> Value of Captured Variable
> ---------------------------
>
> Key: JEXL-426
> URL: https://issues.apache.org/jira/browse/JEXL-426
> Project: Commons JEXL
> Issue Type: Improvement
> Reporter: Xu Pengcheng
> Assignee: Henri Biestro
> Priority: Major
>
> For the code
> {code:java}
> let x = 10;
> function foo() {
> x += 2;
> }
> foo();
> return x;{code}
> By default the return value of x is 10 because `x` inside `foo` is a captured value, re-assign value not affect the variable outside `foo`, which some times confuse the developer, therefore a new feature `constCapture` was introduced to prevent such kind of re-assignment, which is the same behavior of Java code.
> But the problem is variable `x` outside of function is not const, for the following code
>
> {code:java}
> 1: let x = 10;
> 2: function foo() {
> 3: someCall(x);
> 4: }
> 5: x = 20;
> 6: foo();
> 7: return x; {code}
> This code is valid with constCapture = true (similar code in java is invalid because x needs to be 'final'), but the value inside `foo` is 10, not 20, I checked the JEXL source codes and found the value inside `foo` is captured when defining `foo` function (line 3), which is 10, even when calling `foo` in line 6 the value of x is already be 20, this case is also somehow confuse.
>
> I tried extending Interpreter to change the behavior of captured variable to:
> # re-assign the captured variable to latest value from parent frame before executing.
> # populate the value to parent scope while assigning new value to a captured variable
> with this interpreter, the value of x in line 3 and 7 are 22, which I think is more easy to understand.
> {code:java}
> 1: let x = 10;
> 2: function foo() {
> 3: x += 2; // here x = 22
> 4: }
> 5: x = 20;
> 6: foo();
> 7: return x; // here x = 22{code}
>
> Is it possible to add a new feature to support this behavior?
> or provide more apis, i.e.
> # api to get whole frame stack (now I store it in JexlContext)
> # read/write value by variable name for a frame (now I reflect the get/set/getSymbol methods of Frame class)
> # get parent of Scope.
> Thanks very much!
--
This message was sent by Atlassian Jira
(v8.20.10#820010)