I need some explaination about arguements assignment
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 -- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
I am quite at loss to really understand the purpose of the code written...if you can show in some manner what are you trying to achieve some pointers can be given in the correct direction. As is I presume the code will need to rewritten completely.. On Thu, Aug 7, 2014 at 8:43 AM, aria2end <aria2end@gmail.com> wrote:
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
-- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
The function is not complete and it doesn't do anything. I tried to write a recursive function to get value of each node of my tree structure, put it in a String and return it in a proper format so later show it on the transcript but I couldn't never done that and now I rewritten it completely in a different way but still I cant understand what is happening here. The line below just iterates through an OrderedCollection and concatenate each node level to aText : LoL lols do: [ :each | aText := aText , each level , (String with: Character cr) ]. But as soon as I include that line in my function and the debugger reaches to [ counter <= aNumb ] then I receive this error : " Error: Reading a number failed: a digit between 0 and 9 expected. ". which is strange because I am very sure that I pass a number to aNumb but now aNumb holds the value of aText !! It doesn't give me any error when I copy aText value in copyText and use copyText instead of aText. I can't see what is the problem with using aText directly. -- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
Hi Aria, Welcome to Smalltalk and Pharo. I havenât tried executing your code but I have a couple observations. I believe that the problem is in attempting to assign to a passed-in argument. As far as I know, the following is illegal: foo: aString aString := aString , â barâ. Method (and block) arguments may be sent messages, but may not be assigned. A common approach to what it appears you are doing is to use a WriteStream. For example: startRecursion | stream | stream := WriteStream on: String new. self recurseOnStream: stream level: 4. Transcript show: stream contents. recurseOnStream: aStream level: anInteger anInteger <= 0 ifTrue: [^self]. anInteger timesRepeat: aStream tab. aStream nextPutAll: anInteger printString; cr. self recurseOnStream: aStream level: anInteger - 1. Let us know if this helps resolve the issue. James Foster On Aug 6, 2014, at 8:13 PM, aria2end <aria2end@gmail.com> wrote:
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
-- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
Hi james, Your post not only resolved the issue but also introduced me a new approach which I was looking for it. Thank you do you know the reason why assigning to a passed-in argument is illegal by any chance ? -- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
ok something your post does bring up intrinsically not ok with Pharo, but not threatening a bug, as in normal code practice: a) We should never assign values into the arguments.. On compiling .. As I see it, the Pharo compiler makes "aText" now a temp variable of the block ( actually the while loop is not relevant .. ) The argument "aNumb" actually takes on the value of the first argument viz: of the passed aText.. It would be nicer if the compiler refuses to accept assignment to an argument of the method.. As for suggestion that you liked, I would suggest simply run through Pharo By Example first thoroughly... and then take up as much of test runs through existing applications to understand some of the concepts of how to code.. On Thu, Aug 7, 2014 at 10:08 AM, aria2end <aria2end@gmail.com> wrote:
Hi james,
Your post not only resolved the issue but also introduced me a new approach which I was looking for it. Thank you
do you know the reason why assigning to a passed-in argument is illegal by any chance ?
-- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
See enclosed screen shot.. On Thu, Aug 7, 2014 at 11:59 AM, S Krish <krishnamachari.sudhakar@gmail.com> wrote:
ok something your post does bring up intrinsically not ok with Pharo, but not threatening a bug, as in normal code practice:
a) We should never assign values into the arguments..
On compiling ..
As I see it, the Pharo compiler makes "aText" now a temp variable of the block ( actually the while loop is not relevant .. ) The argument "aNumb" actually takes on the value of the first argument viz: of the passed aText..
It would be nicer if the compiler refuses to accept assignment to an argument of the method..
As for suggestion that you liked, I would suggest simply run through Pharo By Example first thoroughly... and then take up as much of test runs through existing applications to understand some of the concepts of how to code..
2014-08-07 6:38 GMT+02:00 aria2end <aria2end@gmail.com>:
Hi james,
Your post not only resolved the issue but also introduced me a new approach which I was looking for it. Thank you
do you know the reason why assigning to a passed-in argument is illegal by any chance ?
This is illegal because then you may lose the original argument value and the debugger does not work well any more.
Assigning to an argument should raise an error, but the new compiler does not raise it (it is a known bug).
-- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
That makes sense now. thanks. -- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
You should first take time to improve the readability of your code, do not forget it is your own food. What about starting something like (no idea what you want to do however): recursiveShowLists: aLoL withText: aText tabsNumber: aNumb | counter index copyText | copyText := aText. aLoL lols ifEmpty: [^self]. aNumb timesRepeat: [copyText := copyText, (String with: Character tab)]. index := 1. aLoL lols do: [ :each | copyText := copyText, each level, (String with: Character cr) ] ] Hilaire Le 07/08/2014 11:13, aria2end a écrit :
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
-- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
-- Dr. Geo - http://drgeo.eu iStoa - http://istao.drgeo.eu
I wanted to make my code more readable but I just didn't know how to do that. Your code really helped me to see how this can be achieved. thanks -- View this message in context: http://forum.world.st/I-need-some-explaination-about-arguements-assignment-t... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
participants (5)
-
aria2end -
Clément Bera -
Hilaire -
James Foster -
S Krish