[squeak-dev] Letting Set contain nils?

Igor Stasenko siguctua at gmail.com
Sat Aug 9 00:38:12 UTC 2008


2008/8/9 tim Rowledge <tim at rowledge.org>:
> Sets are defined as not holding nils.

That's the problem. A nil is just an object. What other reasons
besides implementaion limitations restricts from putting nils in set?
Sets are commonly used as a sets of _arbitrary_ objects. But hey - nil
is an object too. So what the point in such discrimination?


> Changing that would mess up quite a bit of code I suspect.

I suspect not.
Currently Sets will throw an error if you try to include nil as
element. This means that in current system there are no sets
containing nils and no code which intentionally put nils to sets,
because this leads to error.
>From the above i can conclude that removing limitation of inclusion
nils to sets will not break anything in current system.

>
> Sets are sparsely populated arrays (around 40% IIRC) that use the nils to
> mark the unused slots. How do you imagine that we would mark unused slots
> (which are an important part of the whole sparse array thing) if nils were
> allowed?

I thought i outlined possible solutions good enough :)


> And how would you expect #do:/collect: etc to work  - you would
> have to make all your blocks test for nil. Oh and how would code
> discriminate between nils you put in the array and nils that are put there
> by  the system for the sparseness management?
>

Let me illustrate you:

Collection subclass: #Set
	instanceVariableNames: 'tally array containsNil'

Set>>initialize
     containsNil := false


Set>>add: newObject
    newObject ifNil: [ containsNil := true. ^ nil ].  " instead of
self error: blabla ... "
    ... blabla...

Set>>do: aBlock
   containsNil ifTrue: [ aBlock value: nil ].
  ... the rest of method is same as previous  ..

Set>>collect: aBlock
	| newSet |
	newSet := Set new: self size.
	array do: [:each | each ifNotNil: [newSet add: (aBlock value: each)]].
        containsNil ifTrue: [ newSet add: nil ].
	^ newSet

so, its actually a question of adding  'containsNil ifTrue: [] '
everywhere it needs to be.

> If you need Sets and want a distinguished value, make one. Just don't mess
> up the whole system in the process, eh?
>
> tim
> --
> tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
> Strange OpCodes: RDR: Rotate Disk Right
>


-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list