I get an output I wouldn't expect

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

I get an output I wouldn't expect

ZuLuuuuuu
Hello,

The output of the code below:


a := OrderedCollection new.

1 to: 3 do: [:number |
        a add: [number printString printNl].
].

(a at: 1) value.
(a at: 2) value.
(a at: 3) value.


is:


'4'
'4'
'4'


I would expect it to be:


'1'
'2'
'3'


I modified the code like this:


a := OrderedCollection new.

1 to: 3 do: [:number |
        | tempNumber |
       
        tempNumber := number.
       
        a add: [tempNumber printString printNl].
].

(a at: 1) value.
(a at: 2) value.
(a at: 3) value.


And I now get my expected value.

What is the logic here? Is there a more elegant way that you would suggest to get the expected output?
Canol Gökel
Reply | Threaded
Open this post in threaded view
|

Re: I get an output I wouldn't expect

Paolo Bonzini-2
On 09/13/2010 04:36 PM, ZuLuuuuuu wrote:

>
> Hello,
>
> The output of the code below:
>
>
> a := OrderedCollection new.
>
> 1 to: 3 do: [:number |
> a add: [number printString printNl].
> ].
>
> (a at: 1) value.
> (a at: 2) value.
> (a at: 3) value.
>
>
> is:
>
>
> '4'
> '4'
> '4'
>
>
> I would expect it to be:
>
>
> '1'
> '2'
> '3'
>
>
> I modified the code like this:
>
>
> a := OrderedCollection new.
>
> 1 to: 3 do: [:number |
> | tempNumber |
>
> tempNumber := number.
>
> a add: [tempNumber printString printNl].
> ].
>
> (a at: 1) value.
> (a at: 2) value.
> (a at: 3) value.
>
>
> And I now get my expected value.
>
> What is the logic here? Is there a more elegant way that you would suggest
> to get the expected output?

It's a known bug.  Mind reporting it at smalltalk.gnu.org?

Your workaround is the right way to fix it.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk