[Pharo-dev] WeakValueDictionary: shouldn't be "absent" after GC?

Denis Kudriashov dionisiydk at gmail.com
Tue Feb 21 04:37:21 EST 2017


Hi.

2017-02-21 9:44 GMT+01:00 Guillermo Polito <guillermopolito at gmail.com>:

> Hi Dr. Dias,
>
> a WeakValueDictionary get's its values collected if they are not
> referenced strongly. However, the association remains there. This means
> that when you do:
>
> dictionary := WeakValueDictionary with: 'hello' -> nil copy.
>
> You'll have something like this:
>
> WeakValueDictionary {
>   WeakValueAssociation { key: 'hello', value: yourObject }
> }
>
> Once you garbage collect, the value is holding yourObject weakly, so
> you'll have:
>
> WeakValueDictionary {
>   WeakValueAssociation { key: 'hello', value: nil }
> }
>
> But the WeakValueAssociation will still be there.
>
>
For me what you describe is implementation details and users should not
think about it at all.
I agree ifAbsent~ messages should work with collected values by "absent
branch". It is intuitive.

To support it correctly we can use approach of Set which uses #asSetElement
to store items. Then we can easily distinguish nil from absent value. Real
nil value will happen only by garbage collection.
But I am wondering WeakSet not uses SetElement approach directly and
instead it uses special flag object. Maybe it should be fixed too.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20170221/afbd5b88/attachment.html>


More information about the Pharo-dev mailing list