[BUG] WeakKeyDictionary>>keysAndValuesDo:

Chris Muller afunkyobject at yahoo.com
Tue Jun 8 22:27:13 UTC 2004

The following code demonstrates that keysAndValuesDo: will evaluate nil keys
that have been gc'd.

	| d |
	d _ WeakIdentityKeyDictionary new
		at: 'hello' copy put: nil;
	Smalltalk garbageCollectMost.
	d keysAndValuesDo: [ : k : v | k ifNil: [ self halt ] ]

I consider this a bug because it contradicts the notion that nil cannot be a
key in a Dictionary.  Incidentally, keysDo: passes the test.

Wow, so does this:

	Dictionary new at: nil put: 'my nil element'; yourself

Inspecting this even shows the nil in the Dictionary inspector!  Has Dictionary
been enhanced to support nil keys?  Maybe it isn't a bug..??


	Set new add: nil; yourself

"Sets cannot meaningfully contain nil as an element."

It looks like the solution is to override Dictionary>>associationsDo: in
WeakDictionary to:

	associationsDo: aBlock 
		"Evaluate aBlock for each of the receiver's elements (key/value 	

		super associationsDo: [ : eachAssoc | eachAssoc key ifNotNil: [ aBlock value:
eachAssoc ] ]


More information about the Squeak-dev mailing list