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

Igor Stasenko siguctua at gmail.com
Fri Aug 2 13:30:16 UTC 2013


On 2 August 2013 15:15, Frank Shearar <frank.shearar at gmail.com> wrote:
>
> On 2 August 2013 14:08, Max Leske <maxleske at gmail.com> wrote:
>>
>> 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 ]
>
> "dict" should be "collection".
>

Max, can you rewrite the code sample, because this line:

dict add: [ methodScope ]

adding closure to collection?
or should it be:

dict add: [ methodScope ] value
?

>> 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?
>
> While weird, it looks right. Each iteration you store the index in
> methodScope. In the collection you store a block that deferences
> methodScope. After the second iteration, methodScope = 2, so in both
> blocks, the derefence yields 2.
>
> frank
>
>> Cheers,
>> Max



-- 
Best regards,
Igor Stasenko.


More information about the Vm-dev mailing list