[comp.object] Generic Queues

gjditchfield@watmsg.waterloo.edu (Glen Ditchfield) (11/12/89)

In article <654@nastar.UUCP>, joel@nastar.UUCP (Joel Rives) writes:
> ... It is not at all difficult to code a QUEUE object in C++ that will
> accept a generic reference upon which to operate. A new instantiation of
> this QUEUE object could be passed a comparison function when created that
> would be specific to the data type being queued.

In article <317@shrike.AUSTIN.LOCKHEED.COM> aihaug@AUSTIN.LOCKHEED.COM (Daniel A Haug) writes:
>For many of our particular applications, this is overly restrictive.
>I may very well want a single queue to hold objects of vastly different
>types.  Perhaps I'm wrong (and often am ;-), but I don't see how to
>accomplish this in a statically typed language.

The "data type being queued" can be a supertype instead of a specific,
"concrete" type; then the queue can contain objects of vastly different
types that are subtypes of that type.
   Let me put it this way: no matter how vastly different the types of the
elements are, you want to do _something_ to them when you take them off of
the queue!  Lets say you might apply the bleen() and gaggle() operations.
In a dynamically typed language, you would declare a queue-of-anything,
promise yourself that you will never put an object on the queue unless it
has both operations, and hope that testing will uncover any mistaken
enqueues before the program ships.  In a statically typed language, you
would declare type Moof with overridable bleen() and gaggle() operations
and declare a queue-of-Moof.  If you mistakenly enqueue something that
isn't a subtype of Moof, you get a compile-time error.
   I've done this in C++; I have a macro that, given a class E, generates a
class of queue that can contain objects of any type descended from E.  I
gain a static assurance that a certain class of bug is absent from my
program, but I do lose some of the flexibility of pure dynamic typing.  I
find that I don't miss the lost flexibility much, and I can imagine
statically checked type systems which would restore some of that flexibility.

    Glen Ditchfield  gjditchfield@violet.uwaterloo.ca  Office: DC 2517
Dept. of Computer Science, U of Waterloo, Waterloo, Ontario, Canada, N2L 3G1
		Moof! is a trademark of Apple Computer Inc.