comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: How to Use Abstract Data Types
Date: 1998/05/06
Date: 1998-05-06T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.98May6180441@spectre.mitre.org> (raw)
In-Reply-To: 6ilt69$j0g$1@nnrp1.dejanews.com


In article <6ilt69$j0g$1@nnrp1.dejanews.com> adam@irvine.com writes:

  > I'm confused about how this should work...

   Yes, you are.

  > Anyway, I'm listing my entire test program below.  Hopefully, it
  > should be easy to see what I'm trying to accomplish; maybe you or
  > someone else can figure out how I can do it correctly...

   The point where you make the key mistake is right at the beginning:
  
  > package Library_Package is

  >     type Library is abstract tagged null record;
  >     type Book is abstract tagged null record;

   I could show you how to make it work from this point, but that
would be counterproductive. If you put the type Book in a different
package, then the rest is easy.  (And if, when you are finished you
want to recombine the two packages, the language won't stop you.  It
is just that once you confuse types like this you get confused.)  The
language problem is that with this construction you can't have a
function for Library that returns a Book:

  RM 3.9.2(12): "A given subprogram shall not be a dispatching
                 operation of two or more distinct tagged types."

   If you use the correct package layout above, then:

   function Find(L: in Library; Key: in String) return Book;

   should get you the right error message.  Since Book is abstract,
you can't return one.

   Now it appears that you need to create a doubly dispatching
operation.  One common case is where two different parameters of the
same type may have different tags.  (You create a dispatching
operation with one dispatching parameter and one class wide parameter,
which calls another operation which dispatches on the other
parameter.)  Much more usual is the case that is needed here.  You
need to declare operations of Library with class-wide Book parameters:

   function Find(L: in Library; Key: in String) return Book'CLASS;

   or whatever.  Notice that Find if it is defined in the same package
as Library will be a dispatching operation of Library.  Find may call
operations of Book which will dispatch to the operation for that
particular type of Book, but that is transparent when writing Find.

    Find is not a dispatching operation of Book, but since the
operations it calls on Book can dispatch, it behaves like one.  As I
said elsewhere, this ability to disentangle unrelated classes is
not unique to Ada.  However, in Ada it is if not required at least
strongly encouraged by the structure of the language.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1998-05-06  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-04  0:00 How to Use Abstract Data Types adam
1998-05-06  0:00 ` Robert I. Eachus [this message]
  -- strict thread matches above, loose matches on Subject: below --
1998-04-30  0:00 adam
1998-05-06  0:00 ` Robert I. Eachus
1998-04-22  0:00 adam
1998-04-23  0:00 ` Jacob Sparre Andersen
1998-04-30  0:00 ` Robert I. Eachus
     [not found]   ` <matthew_heaney-ya023680003004981709380001@news.ni.net>
1998-05-05  0:00     ` Stephen Leake
1998-05-05  0:00       ` Matthew Heaney
1998-05-06  0:00         ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox