Convert to pass-by-reference instead of pass-by-value #678
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Beyond toy programs, the translation of Alan code needs to normally be pass-by-reference under the hood in Rust so variable names remain available after being used on a function call. Most developers would not expect their variable name to no longer be valid after they provided it to a function.
Further, some functions will need to be mutable references to allow mutation of a provided variable. This can make the state of your own code harder to follow since your own variables may be mutated by the functions you're calling, instead of returning a new variable with a new value, but in certain situations it is both more performant and is expected, for instance with arrays/vectors. How the clarity of mutable variables is dealt with is beyond this PR, and also probably beyond the first major demo, since I can get away with binding Rust functions that have mutable arguments and not worry about how this will be handled in pure Alan code for now.
At the moment, I'm butchering the intent of pass-by-reference by then immediately
.clone()
ing every argument passed in for pure-Alan functions (but also not a problem with the bound functions). I figure later I will introduce a ref/val flag on variable names in the Microstatements, as well as a mutability flag (which is partially implemented) that may or may not be used to inform function generation. But this PR has gotten large enough as it is, so I figured it's best to stop here now that the test suite is back to the watermark onmain
.