I need some explaination about arguements assignment
Posted by aria2end on Aug 07, 2014; 3:13am
URL: https://forum.world.st/I-need-some-explaination-about-arguements-assignment-tp4772102.html
Hello, I am new to smalltalk and I would really appreciate your help so I can better use and understand Pharo.
So here is the problem, I wanted to implement a function to get the value of my tree struct nodes in a recursive manner and I wanted to concatenate them to a String ( aText ) and later show them on the Transcript.
showLists
| aText |
aText := lol level, (String with: Character cr).
self recursiveShowLists: lol withText: aText tabsNumber: 1.
recursiveShowLists: aLoL withText: aText tabsNumber: aNumb
| counter index |
aLoL lols isEmpty
ifFalse: [
counter := 1.
[ counter <= aNumb ]
whileTrue: [
aText := aText , (String with: Character tab).
counter := counter + 1 ].
index := 1.
aNumb := aNumb + 1.
LoL lols do: [ :each | aText := aText , each level , (String with: Character cr) ].
]
Without having the last line ( LoL lols do: ... ) everything works fine but when I include it and as soon as debugger reaches to [counter <= aNumb ] and wants to evaluate it, my arguments start to act weird. aText will become nil and aNumb will hold te value of aText!
I fixed this issue by adding another temporary variable ( copyText ) and instead of direct assignment of aText I used the copyText:
recursiveShowLists: aLoL withText: aText tabsNumber: aNumb
| counter index copyText |
copyText := aText.
aLoL lols isEmpty
ifFalse: [
counter := 1.
[ counter <= aNumb ]
whileTrue: [
copyText := copyText , (String with: Character tab).
counter := counter + 1 ].
index := 1.
aLoL lols do: [ :each | copyText := copyText , each level , (String with: Character cr) ] ]
I really like to know the reason behind this issue, I appreciate all the explanations.
Thanks,
Aria