rlwhitcomb / utilities

Some of my personal utility programs
MIT License
2 stars 0 forks source link

Need a way to pass parameters by reference in Calc #645

Open rlwhitcomb opened 8 months ago

rlwhitcomb commented 8 months ago

Say I want to pass in a list/array or map/object to be modified inside a function. Currently that doesn't work:

list = [] -> [ ]
Defining function addTo(a, n) = {
   a += n
}
addTo(list, 1)
Error: Cannot change Parameter value a at line 3.

Suggest allowing var or const (default) as a modifier in front of the name, as in:

define a(var a, const n) = { ... }

where the default would be const (as now), but either one is allowed in order to be explicit.

Of course, this would imply that the actual value for a non-const parameter could not be a constant value...

rlwhitcomb commented 8 months ago

Okay, apart from the difficulties I'm having just implementing this, there is at least one (likely) insoluble problem: when an empty set/map is converted to one or the other when the first element is added. Even in the best case when I could get the real object and make a new one, the original reference (in the symbol table of one of the callers) is never referenced anywhere, so I could never update the original reference with the new object, unless I could pass in the original LValueContext of it ... (good luck with that). But, then again, maybe this is the crux of the whole situation: passing a valid LValueContext around as the "value" of the parameter. I don't seem to be "getting" it yet ....

rlwhitcomb commented 7 months ago

Questions: