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

2015-09-21 Thread Peter W A Wood
Thanks very much for the explanation Mark.

Regards

Peter

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

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

2015-09-21 Thread Peter W A Wood
Dave

> On 21 Sep 2015, at 16:15, Dave Kilroy  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
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


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

2015-09-21 Thread Dave Kilroy
Peter & Mark - hooray! :)



-
"The difference between genius and stupidity is; genius has its limits." - 
Albert Einstein
--
View this message in context: 
http://runtime-revolution.278305.n4.nabble.com/Is-it-safe-to-return-an-array-from-a-function-tp4696483p4696496.html
Sent from the Revolution - User mailing list archive at Nabble.com.

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


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

2015-09-21 Thread Mark Waddingham
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  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@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@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

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

2015-09-21 Thread Dave Kilroy
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 difference between genius and stupidity is; genius has its limits." - 
Albert Einstein
--
View this message in context: 
http://runtime-revolution.278305.n4.nabble.com/Is-it-safe-to-return-an-array-from-a-function-tp4696483p4696488.html
Sent from the Revolution - User mailing list archive at Nabble.com.

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


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

2015-09-21 Thread 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 difference between genius and stupidity is; genius has its limits." - 
Albert Einstein
--
View this message in context: 
http://runtime-revolution.278305.n4.nabble.com/Is-it-safe-to-return-an-array-from-a-function-tp4696483p4696486.html
Sent from the Revolution - User mailing list archive at Nabble.com.

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Is it safe to return an array from a function?

2015-09-20 Thread 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
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode