[Feature Request] Variable-Capturing Functions #543
Replies: 2 comments
-
|
This code does not compile because runtime (CLEO) does not have access to outer variables. When the code enters the function scope, local variables space (32x4 bytes) is saved and the function uses for its purposes. 0@ in a function and in the main scope points to the same memory address. Capturing outer variables is impossible without significant backend changes. |
Beta Was this translation helpful? Give feedback.
-
|
With that said, I do like the idea as it unlocks some interesting patterns, like function cache. There was an idea of expanding local variable space (see sannybuilder/dev#270) which may help to implement it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Suggestion
Howdy, folks. I was thinking if it's possible to implement variable-capturing functions, which means functions should be able to "capture" variables declared earlier outside their scope. This works exactly like functions that don't specify the
statickeyword in C#.The Problem
Take the sample script below as an example.
The code above does not compile, as the functions don't see the variables declared in the main scope. There are two primary methods to fix this:
scriptPlayerandplayerCoordsto both functions every time, which clutters code unnecessarily, especially as more complex functions or function arguments are added.The Solution
Add a keyword to make functions capture variables, similar to PHP. Requires minimal code changes on your side (hopefully). Functions are non-capturing by default and users can choose what to capture via the optional
usekeyword, e.g:Optional Enhancement
Let the user choose whether they want to pass captured arguments by value or reference (so that functions can modify them). Another welcome addition is to add this to regular parameters as well (like the
valueparameter below).Please let me know what you think about this suggestion. Keep up the great work!
Beta Was this translation helpful? Give feedback.
All reactions