From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6b85f5fd782a1527 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: How to Use Abstract Data Types Date: 1998/05/06 Message-ID: #1/1 X-Deja-AN: 350972723 References: <6ilt69$j0g$1@nnrp1.dejanews.com> Organization: The Mitre Corp., Bedford, MA. Newsgroups: comp.lang.ada Date: 1998-05-06T00:00:00+00:00 List-Id: 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...