Login  Register

Re: I need some explaination about arguements assignment

Posted by jgfoster on Aug 07, 2014; 4:09am
URL: https://forum.world.st/I-need-some-explaination-about-arguements-assignment-tp4772102p4772105.html

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 <[hidden email]> 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-tp4772102.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>
>