[Vm-dev] modified method temp references in closures: possible bug?
Lou at Keystone-Software.com
Fri Aug 2 13:32:20 UTC 2013
Hi Max and Igor,
First Igor, I'm sure Max meant collection and not dict, probably just a
typo caused by playing around.
Max, this is not a bug (other than in your test),
collection add: [ methodScope ] doesn't (and shouldn't) add a block with
the value of methodScope at the time of the add: but adds a block with (for
lack of a better term) a pointer to the methodScope variable. When the
block is executed (sent #value) the value of the block is the current value
of methodScope, in your case 2. Try this:
| methodScope |
collection := OrderedCollection new.
1 to: 2 do: [ :index |
methodScope := index.
collection add: [ methodScope ] ].
methodScope := 5.
collection do: [ :block | Transcript show: block value; cr ]
>On 2 August 2013 15: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):
>> | methodScope |
>> collection := OrderedCollection new.
>> 1 to: 2 do: [ :index |
>> methodScope := index.
>> dict add: [ methodScope ] ].
>> collection do: [ :block | Transcript show: block value; cr ]
>err.. i see
>but where dict was declared?
>> 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?
Keystone Software Corp.
mailto:Lou at Keystone-Software.com http://www.Keystone-Software.com
More information about the Vm-dev