Weird call by reference behavior

Archives Forums/BlitzMax Bug Reports/Weird call by reference behavior

LordChaos(Posted 2011) [#1]
I just noticed some weird issue when I use a call by reference and function with a for each loop. To me, this is very irritating. Is this code supposed to be behave this way?


SuperStrict


Local strings:String[] = ["test1", "test2", "test3"]

' shoudln't these loops have the same semantics?
For Local i:String = EachIn strings
	Change(i)
Next
'For Local i:Int = 0 Until strings.length
'	Change(strings[i])
'Next

For Local i:String = EachIn strings
	Print i
Next

Function Change(str:String Var)

	str = "foobar"
	
End Function



SebHoll(Posted 2011) [#2]
I doubt it - i in the eachin loop is not a reference to the original string[i] element.


Azathoth(Posted 2011) [#3]
i:String contains a copy of each element, not a reference.

This does the same thing with element 0;
SuperStrict


Local strings:String[] = ["test1", "test2", "test3"]

Local i:String=strings[0]

Change(i)

Print strings[0]
Print i

Function Change(str:String Var)

	str = "foobar"
	
End Function


Last edited 2011


GW(Posted 2011) [#4]
i:String contains a copy of each element, not a reference.

This is true only for strings?


Azathoth(Posted 2011) [#5]
And integers/floats.

i is being passed by reference to Change, so the contents of i is being changed not strings

Last edited 2011