Array by reference wierdness

Monkey Forums/Monkey Programming/Array by reference wierdness

charlie(Posted 2012) [#1]
This works:

Function vecAbs:Void(dest:Float[],a:Float[])
     dest[0]=Abs(a[0])
     dest[1]=Abs(a[1])
     dest[2]=Abs(a[2])
end


This doesn't:

Function vecAbs:Void(dest:Float[],a:Float[])
     dest=[Abs(a[0]),Abs(a[1],Abs(a[2])]
end


That doesn't seem right, right?

Cheers
Charlie


Gerry Quinn(Posted 2012) [#2]
Arrays are immutable (though their contents can change). It's similar to what happens when you pass a string as a parameter to a function and try to change it.


charlie(Posted 2012) [#3]
So that's expected behaviour then?

What's the reason for not being able to pass by reference, sure;y it's available on all the main targets?

Cheers
Charlie


Gerry Quinn(Posted 2012) [#4]
I suspect it's a combination of technical reasons and preference.

You could always wrap your array in an object if you want to do this. This would enable you to have a null valued 'array object' too, another thing which arrays as currently constituted don't do.


marksibly(Posted 2012) [#5]
Hi,

I suspect what you're after is 'Var' params ala BlitzMax, eg:

Function vecAbs:Void( dest:Float[] Var,a:Float[] )

But this is not supported on many targets - C++/C# are the only ones I think.