[Glass] Dictionaries/Set supporting nil

Martin McClure martin.mcclure at gemtalksystems.com
Mon Nov 25 10:52:14 PST 2013


On 11/24/2013 10:56 AM, Mariano Martinez Peck wrote:
> Hi guys,
> 
> I have already faced lots of times the fact that I add some nils in a
> set or a nil key in a dictionary.. the workaround from the app side are
> kind of a hack..i wonder if there are some other workaround directly in
> GemStone? Is it planned to support this in the future?
> 

It's traditional for hashed collections in Smalltalk to use nil as the
marker for an unused slot in the hash table, with the result that nil
cannot be stored as a key in the table. The base classes in GemStone
follow this tradition, as do the ones in Pharo.

However, it's pretty easy to implement your own set or dictionary that
allows nil keys in the hash table. Just create a unique token object
(can be anything, an instance of Object works fine), hold it in an
instvar, initialize the hash table to be full of references to that
object instead of to nil, and in the hash table logic compare by
identity to the token instead of to nil. Such an object performs just as
well as a normal Set or Dictionary except during initialization and
growing, since the initialization of the hash table has to be done twice
-- once to nil by the basicNew primitive and then again to refer to the
token object.

Regards



More information about the Glass mailing list