Igor Stasenko wrote:
> 2009/12/31 Martin McClure <[email protected]>:
>> If you run this in 1.1:
>>
>> | dict |
>> dict := WeakKeyDictionary new.
>> dict at: 'foo' copy put: 'bar'.
>> Smalltalk garbageCollect.
>> dict associationsDo: [:assoc | self halt].
>> dict do: [:val | self halt]
>>
>>
>> It stops in the second halt, but not the first.
>>
>> This is because #associationsDo: does not include associations whose
>> keys have been GCed, but #do: *does* include values whose corresponding
>> keys have been GCed.
>>
>> This behavior seems inconsistent, and not particularly useful, and also
>> seems to contradict the comments by the author in the Squeak bug from
>> 2007, from which I'd think that #do: would not include the value which
>> it currently does.
>>
> It is useful in terms, that sometimes you don't want to see the
> objects which GCed,
> but sometimes do. See how WeakRegistry using this.
> The lack of ephemerons support in VM, forcing us to deal with such
> things manually.

I see the value in being able to choose both. WeakRegistry uses
#allAssociationsDo:, not #associationsDo:. Seems like it would be more
consistent to also have an #allDo: that includes values of collected
keys, and a #do: that does not.

> 
>> So I *think* this is an oversight, and is easy to fix by defining
>> WeakKeyDictionary>>do: in terms of self associationsDo:.
>>
>> But then there's the question of #size. The collection above answers 1
>> to #size, which also seems wrong.
>>
> #size for weak key dict is non-deterministic. Because even if you want to know
> the exact number of elements, you will need to iterate over the
> collection, but then,
> iteration can trigger GC, and you have a big chances to answer wrong value.

That seems reasonable, though there's no effort to even come close in size.

-Martin

_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply via email to