[Cuis-dev] Generality number vs Double dispatch

Ces VLC cesarillovlc at gmail.com
Sat Jan 2 04:17:20 PST 2021


Yes, Luciano, now I did read the original 1986 minimalist paper by Ingalls
about double dispatching (the one which is just 3-pages long), and the
solution is elegant and clean. So, my disappointing is not about the
technique, but about  applying it for type conversion. I mean, when a
message is polymorphic in more than one variable (like in the example with
geometric primitives and display devices that Ingalls uses in his paper),
multiple dispatching is a clean and tidy approach when, for example, you
use it for finding the actual implementation for drawing a rectangle on a
printer, a circle on the screen, etc, so you avoid case trees that wouldn't
be clean nor easy to maintain.

What I don't like is when this technique is used just for the purpose of
type conversion: You don't have a different implementation for, say, adding
an integer and a float. In binary arithmetic messages, multiple dispatching
is not used for locating the proper implementation, because you have only
one implementation (ie: integer+integer, float+float, etc). So, you use
multiple dispatching as a trick for type conversion. That's not clean (it
has nothing in common with the semantics used in the Ingalls paper), and,
it's not efficient either (usually non-clean designs lead to inefficient
solutions), as you incur in the penalty of two message lookups
(indirections) for just the purpose of type conversion. The clean solution
here, IMHO, is to make the programmer responsible for the types of objects
in arithmetic binary messages and do any needed type conversions explicit.
Alternatively, if the syntax of the expression must be free of explicit
type conversion because of any design criteria, then the generality number
approach doesn't look as untidy as double dispatch to me.

Thanks a lot for your comments, and kind regards,

César



On Sat, Jan 2, 2021 at 6:20 AM Luciano Notarfrancesco <luchiano at gmail.com>
wrote:

> IMO the double dispatch mechanism is pretty elegant, and I found it to
> work well even after extending Smalltalk to cover much more of mathematics.
>
> What I found more important is to make sure that every element has
> “parent”, an algebraic structure it belongs to. Then, if I try to do ‘a *
> b’ with a and b in two different rings A and B for example, the double
> dispatch mechanism looks for a canonical ring homomorphism A -> B or B -> A
> to convert one of the elements, and then resends the message with both
> elements in the same ring, works perfectly.
>
> Anyway, this is just implemented for convenience, in truth I don’t really
> need it and I could just raise an exception when trying to multiply two
> elements of different rings.
>
> In the base image there’s a little annoyance because of the automatic
> conversion of x/1 fractions to the integer x. I ended up creating a new
> class Rational and avoiding Fraction in my code, but it’s not a big deal
> for me, and the way Fractions work in the base image seems to be what most
> people expect.
>
> On Sat, 2 Jan 2021 at 12:52 AM, Ces VLC via Cuis-dev <
> cuis-dev at lists.cuis.st> wrote:
>
>> Hi again!
>>
>> Being completely new to Smalltalk, I learnt about the "double dispatch"
>> technique yesterday, and my first reaction was that I felt quite
>> disappointed, because everything I was learning in Smalltalk was clean,
>> tidy, and efficient, ...until I read about double dispatching.
>>
>> Then I searched the Blue Book for double dispatching and found zero
>> matches. Then I learnt Smalltalk-80 used "generality numbers" instead of
>> double dispatching. With a bit more of searching, I found Cuis uses double
>> dispatching because that change was done in Squeak by Ingalls time ago.
>>
>> When reading the "generality numbers" approach in the Blue Book, I don't
>> feel disappointed at all: it looks to me like the clean and obvious
>> solution to the problem (ie: binary operators should be legal for objects
>> of the same class only, and trying to perform a binary on different classes
>> should trigger an error... however, if for some reason you want Smalltalk
>> to do the conversion for you, the generality number approach doesn't break
>> tidiness and pure design).
>>
>> But I have a question, though: Did any of you have a long experience with
>> Smalltalk-80 and found the generality number technique to be a limiting
>> factor? I mean: Were your applications worse written (ie: with worse
>> design) because of not having double dispatch?
>>
>> I'd like to check whether Smalltalk without double dispatching is worse
>> than with it, or not.
>>
>> Thanks a lot,
>>
>> César
>>
>> --
>> Cuis-dev mailing list
>> Cuis-dev at lists.cuis.st
>> https://lists.cuis.st/mailman/listinfo/cuis-dev
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20210102/be9aa8c6/attachment-0001.htm>


More information about the Cuis-dev mailing list