[squeak-dev] intersection: method for dictionaries flaky

Igor Stasenko siguctua at gmail.com
Mon Jul 20 22:07:08 UTC 2009


2009/7/20 Ralph Boland <rpboland at gmail.com>:
> 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.
+1

> 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.
>

c) make Dictionary be not the subclass of Set, then questions, like
wrong #intersection: symmetry will auto-become a non-issue. :)

>
>    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)
>
good idea, to ask from user explicitly, what he has in mind!

>
>  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?

We discussed this topic a while ago on squeak-dev (and i was one, who
raised the same questions which you raising now).
True: in current state, using #intersection: on dictionaries having
not much sense , because it is unclear what behavior we should
imply/expect from it (symmetry , intersections on values, or keys
etc).

> 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? :-)
>
The problem with Dictionaries, is that they are part of ANSI standard
(as other Collections), so, i think we should preserve the protocol in
order to stay compatible with other forks/dialects.
So, removing or deprecating methods in Dictionaries is not so easy
task and requires good consideration , does that change/removal makes
us incompatible with others or not.

> Ralph Boland
>
>



-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list