This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: The Nil Set
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
While the VisualWorks Development list is hot with the parcels topic and comp.lang.smalltalk and various blogs rage with the "type war", there's an interesting discussion about Sets and nil over in the Squeak list. I've never really thought about it, but I'm inclined to say that Set's should be able to contain nil. The expression (Set withAll: (Array new: 5)) size ought to return 1. Not 0. And the expression (Bag withAll: (Array new: 5)) size should return 5. Not 0.
VisualWorks and some of the other mainstream Smalltalks don't allow nil to be used in Sets or Dictionaries. It's not that allowing/disallowing nil is necessarily right, it's that its the easy thing.
When an object that contains other objects is created, all of its slots are populated by default with nil. The VM does this quickly and efficiently. Behind the scenes, a Set is just an Array with some of its slots populated. So to begin with, it's some arbitrary sized array with nil slots. As objects are added, their hash index is computed, and they are placed in the array. When they're removed, the slot is set back to nil. To avoid using nil, we'd have to create another arbitrary object to use to indicate "the empty slot". Then nil could be used as a first class element of a Set. Or as a first class key for a Dictionary which subclasses Set.
I thought I'd look at what it would take to switch VisualWorks. But a report of how that goes, can be its own blog. I find that long blogs are.... well.... long.