[Pharo-dev] [Vm-dev] modified method temp references in closures: possible bug?

Clément Béra bera.clement at gmail.com
Fri Aug 2 09:16:06 EDT 2013


This is not a bug.

Here your variable methodScope is defined outside the block, therefore all
the blocks and the method refers to the same variable when calling
methodScope.

Your code is the same as if you wrote :
| methodScope |
methodScope := 1.
methodScope := 2.
At the end, methodScope is of course 2.

If you want the result :
1
2
you have to declare the variable in the block (then there are different
variable named methodScope, actually 1 per activation of the block.

collection := OrderedCollection new.
1 to: 2 do: [ :index |
        | methodScope |
        methodScope := index.
        dict add: [ methodScope ] ].
collection do: [ :block | Transcript show: block value; cr ]


2013/8/2 Max Leske <maxleske at gmail.com>

>
> I just came across this (possible) bug and I'm able to reproduce it in
> Pharo 2.0 and 3.0 with the latest PharoVM (found in a 1.3 with NBCog):
>
> expected (by my feeble mind):
> 1
> 2
>
> actual:
> 2
> 2
>
> | methodScope |
> collection := OrderedCollection new.
> 1 to: 2 do: [ :index |
>         methodScope := index.
>         dict add: [ methodScope ] ].
>
> collection do: [ :block | Transcript show: block value; cr ]
>
> I'm aware that using method temps in this way is not good programming
> style (usually) but I still think this behavior is curious if not at least
> very likely to cause a lot of weird errors.
>
> Should I open a bug report?
>
> Cheers,
> Max
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20130802/dfc6f717/attachment-0002.html>


More information about the Pharo-dev mailing list