[squeak-dev] intersection: method for dictionaries flaky
Ralph Boland
rpboland at gmail.com
Mon Jul 20 19:01:57 UTC 2009
When I run the following code:
| a b ans |
ans := Array new: 2.
a := Dictionary new.
b := Dictionary new.
a at: 1 put: $a; at: 2 put: $b; at: $3 put: $c.
b at: 1 put: $a; at: 2 put: $x; at: $3 put: $b.
self halt.
ans at: 1 put: (a intersection: b).
ans at: 2 put: (b intersection: a).
ans
I get output:
an Array(a Dictionary(1->$a 2->$b ) a Dictionary($3->$b 1->$a ))
In other words, the "intersection:" method is not symmetric for Dictionaries.
As a mathematician I find this very disturbing.
I propose that the intersection of two Dictionaries should either
a) Return the intersection of the keys of the Dictionaries.
Not an intuitive answer either but better than now.
For example, what would you expect the intersection of two
MethodDictionaries
to be?
b) Generate the shouldNotImplement error.
My preference is for b) as it is straightforward to get answer a) or the
current answer if that is what you want. We could even make methods
for these, say "intersectionKeys:" and
"intersectionValues:" (values are from receiver)
I am asking because I am currently implementing the "intersection:" method
for sets as an optimization and unfortunately my code applies to
dictionaries as well. (And I change the result for dictionaries).
Yet another example of why Dictionary should NOT be a subclass of Set.
Now the tough question: Does, and if so where, Squeak and the many
Squeak packages depend upon the current behavior of "intersection:" with
respect to dictionaries?
Perhaps Dictionary should implement "intersection:" and have it invoke
"intersectionValues". The Dictionary "intersection:" method could
then be marked
as deprecated.
How ofter does a method become deprecated at its time of creation? :-)
Ralph Boland
More information about the Squeak-dev
mailing list
|