Re: wikipedia draft
- Posted by petelomax Jan 11, 2021
- 1699 views
As promised, a complete rewrite:
Parameter passing
Integers are always passed by value, whereas atom/sequence/string are passed by reference with copy-on-write semantics.
That realises the performance benefits of pass-by-reference but with the behaviour of pass-by-value, for example:
function set55(sequence s) s[5][5] = 5 return s end function sequence a = repeat(repeat(0,5),5), -- a 5x5 matrix b = set55(a)
The value of a is not changed by the call, b obviously ends up with a single 5 in the lower right.
In fact it only "clones" the top-level of b and b[5], b[1..4] actually still share the same memory as a,
but will cease doing that if and when further modifications are made to b. It will not need to re-clone
the top-level of b or b[5] again (and now ditto a) but can instead update those non-shared parts in situ.
(The fact that atoms quiely undergo a similar treatment should be of little or no practical concern.)
In contrast, dictionary and struct/class instance parameters are more accurately described as pass-by-reference.
Again the reality is slightly different, for example:
class mytest public string s end class procedure change(mytest c, d) c = new({"c"}) d.s = "d" end procedure mytest a = new({"a"}), b = new({"b"}) change(a,b) ?a.s -- "a" ?b.s -- "d"
In practice, a remains unmodified, because we simply overwrote the reference to it, whereas b is modified
indirectly as and when the field of d is updated. Note that had you instead performed delete(c), then a
would have instantly become meaningless and invalid, and a cannot be nullified within change() other than
by making it a function which returns (c or null), and then explicitly assigning that return back to a.
One other (fairly obvious) true thing to say is that new() creates space to hold values, and you can never
hold more values, that is, in instance variables, than the number of times you have invoked new().
A very similar program but with no classes and all strings could at one point hold four different strings
in a,b,c,d, but that's simply not possible in the above because you've only called new() three times.