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

Martin Dias tinchodias at gmail.com
Tue Feb 21 18:57:41 EST 2017


Hi all

Phil:
I'm sorry, didn't get what you point with the code snippet. The copy of the
Symbol is the same instance, that I think it's nto GCed.

Guille:
Yes, I knew the internals of the WeakValueDictionary but asked about the
API. What I don't know is about using WeakRegistry and Ephemerons. I'll
check WeakKeyDictionary.

I understand the idea of "explicit control of magic", but not sure if I
understand concretely. Do you mean to implement something like
WeakValueDictionary>>register
which will not be executed by #initialize, so the user can decide to
register. Am I right?

Denis:
+1 WeakSet behavior is really confusing! In thte case of
WeakValueDictionary I didn't check for inconsistencies but was only annoyed
but it's behavior.

Guille and Denis:
Instead of using SetElement or cleaning up the empty WeakValueAssociations,
what I first thought is to internally consider an association with nil as
absent. I mean, modify or override some methods soem when there is an
WeakValueAssociation
with value == nil it considers it's absent. Concretely, I'd expect:

| dictionary |
dictionary := WeakValueDictionary with: 'hello' -> 'world' copy.
Smalltalk garbageCollect.
{
dictionary values includes: nil. *---> false *
dictionary at: 'hello'. * ---> NotFound signal*
dictionary at: 'hello' ifAbsent: [ 'absent' ].* ---> 'absent'*
dictionary at: 'hello' ifAbsentPut: [ 'put' ]. *---> 'put'*
}


It's better if I give some context. Look this simplified version of my use
case:

MyUI>>
morphAt: key
cache ifNil: [ cache := WeakValueDictionary new ].
 ^ cache at: key ifPresent: [:cachedValueOrNil | cachedValueOrNil ifNotNil:
[ cachedValueOrNil ] ifNil: [ cache at: entryReference put: (self newMorph:
key) ] ] ifAbsent: [ cache at: entryReference put: (self newMorph: key) ]

I'd like to only send #newMorph: in ifAbsent: and to avoid the
ifNotNil:ifNil:
Like this:

morphAt: key
cache ifNil: [ cache := WeakValueDictionary new ].
 ^ cache at: key ifAbsent: [ cache at: entryReference put: (self newMorph:
key) ]

:-)

Martín
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20170221/df919657/attachment.html>


More information about the Pharo-dev mailing list