Hi guys, based on codes above, seems like List and Str are passed into func/method as value and not reference. Am I correct? If I am, why is it so? I can't seem to find any explanation in the docs.
So if I want to replace the value of the original variable like in Java/C#, how should I do it idiomatically in Fantom?
SlimerDudeWed 25 Jul 2012
Str's are immutable (like in Java) and Lists are passed by ref. What you're doing is re-assigning your local variables v and s and returning them - you're not changing the refs to l1 and s2
if I want to replace the value of the original variable
ikhwanhayat Wed 25 Jul 2012
fansh> alterList := |Int[] v -> Int[]| { v = [1]; return v; } |sys::Int[]->sys::Int[]| fansh> l1 := Int[,] [,] fansh> l2 := alterList2(l1) [1] fansh> l1 [,] fansh> alterStr := |Str s->Str| { s = "altered"; return s; } |sys::Str->sys::Str| fansh> s1 := "test" test fansh> s2 := alterStr(s1) altered fansh> s1 testHi guys, based on codes above, seems like
ListandStrare passed into func/method as value and not reference. Am I correct? If I am, why is it so? I can't seem to find any explanation in the docs.So if I want to replace the value of the original variable like in Java/C#, how should I do it idiomatically in Fantom?
SlimerDude Wed 25 Jul 2012
Str's are immutable (like in Java) and Lists are passed by ref. What you're doing is re-assigning your local variables
vandsand returning them - you're not changing the refs tol1ands2brian Wed 25 Jul 2012
This code:
alterList := |Int[] v -> Int[]| { v = [1]; return v; }You are creating a new Int[] and assigning it to v. You probably want something like this if you want to modify in place:
alterList := |Int[] v -> Int[]| { v.add(1); return v; }ikhwanhayat Wed 25 Jul 2012
Ah, I see it now. I think I got it mixed up with
outparameter in C#.Thanks guys!