Is it safe to return an array from a function?

Peter W A Wood peterwawood at gmail.com
Mon Sep 21 07:03:26 EDT 2015


Thanks very much for the explanation Mark.

Regards

Peter

> On 21 Sep 2015, at 16:52, Mark Waddingham <mark at livecode.com> wrote:
> 
> Like numbers and strings, arrays are values so they are always (notionally) copied whenever they move between locations.
> 
> In 7+ this copying is deferred until needed (copy-on-write), prior to 7 they are copied immediately.
> 
> So, yes, you can pass arrays around without worry.
> 
> Mark.
> 
> Sent from my iPhone
> 
>> On 21 Sep 2015, at 03:28, Peter W A Wood <peterwawood at gmail.com> wrote:
>> 
>> I understand that arrays are passed by reference in LiveCode not by value. I assume that is also that case if you return an array from a function (i.e. the reference to the array is returned not its content). When I tried to return an array from a function, I found that not only you can do so but multiple calls of the function provide different arrays. As the array being returned from the function is local to the function, I’m guessing that LiveCode is creating a new local array for each function call and returning a reference to the local variable.
>> 
>> Here is the code that I ran under LiveCode Server:
>> 
>> function returnArray
>> local a
>> put 1 into a[1]
>> return a
>> end returnArray
>> 
>> put returnArray() into b
>> put b[1] & return                —> prints 1
>> 
>> put returnArray() into c
>> put c[1] & return                —> prints 1               
>> 
>> put 3 into c[1]
>> 
>> put b[1] & return                —> prints 1
>> put c[1] & return                —> prints 2
>> 
>> Is it safe to use such a technique or will the reference to the array become invalid at some stage?
>> 
>> Peter
>> 
>> 
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list