Is it safe to return an array from a function?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Is it safe to return an array from a function?

Peter W A Wood
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
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Is it safe to return an array from a function?

Dave Kilroy
Hi Peter

Your scary posting made me test on LC8.0(db4), LC7.0.1(rc2) and LC 6.7.6 - and I'm relieved to say that the arrays performed impeccably (returning '3' in the final line)

Can you give us some more info on your setup and anything else that could be conspiring to give you '2' instead of 3?
"The first 90% of the task takes 90% of the time, and the last 10% takes the other 90% of the time."
Peter M. Brigham
Reply | Threaded
Open this post in threaded view
|

Re: Is it safe to return an array from a function?

Dave Kilroy
In reply to this post by Peter W A Wood
Peter - looking at your post again makes me think your 'returns 2' must have been a typo (phew) - let me know if not...

Page 8 of the Release Notes for LC 7.0.0(rc2) states:

Array element pass by reference (7.0.0-rc-2)

It is now possible to pass parts of an array by reference. For example, the following

on mouseUp local tArray
put "" into tArray[1][2] passByRef tArray[1]
put tArray[1][2]
end mouseUp

on passByRef @rArray
put "changed" into rArray[2]
end passByRef

in the script of a button will result in "changed" appearing in the message box when the button is pressed.

This allows users to reduce the overhead associated with passing sub-arrays to handlers, as this would no longer require copying the sub-array internally.
But this says nothing about arrays returned from functions - so you have a VERY GOOD QUESTION - at a guess I should think that the well-established 'return' routines would trump any new-fangled 'referencing' functionality (otherwise how many apps would suddenly start crashing?).

But it would indeed be nice to get confirmation from someone who knows...

Kind regards

Dave
"The first 90% of the task takes 90% of the time, and the last 10% takes the other 90% of the time."
Peter M. Brigham
Reply | Threaded
Open this post in threaded view
|

Re: Is it safe to return an array from a function?

Mark Waddingham-2
In reply to this post by Peter W A Wood
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 <[hidden email]> 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
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Is it safe to return an array from a function?

Peter W A Wood
In reply to this post by Dave Kilroy
Dave

> On 21 Sep 2015, at 16:15, Dave Kilroy <[hidden email]> wrote:
>
> Hi Peter
>
> Your scary posting made me test on LC8.0(db4), LC7.0.1(rc2) and LC 6.7.6 -
> and I'm relieved to say that the arrays performed impeccably (returning '3'
> in the final line)
>
> Can you give us some more info on your setup and anything else that could be
> conspiring to give you '2' instead of 3?

The main difference between my set up and yours is the keyboard operator driver. I typed 2 when I should have typed 3. Sorry if I got you worried.

Regards

Peter


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Is it safe to return an array from a function?

Peter W A Wood
In reply to this post by Mark Waddingham-2
Thanks very much for the explanation Mark.

Regards

Peter

> On 21 Sep 2015, at 16:52, Mark Waddingham <[hidden email]> 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 <[hidden email]> 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
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Is it safe to return an array from a function?

Dave Kilroy
Peter & Mark - hooray! :)
"The first 90% of the task takes 90% of the time, and the last 10% takes the other 90% of the time."
Peter M. Brigham