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=0.8 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT,REPLYTO_WITHOUT_TO_CC,T_FILL_THIS_FORM_SHORT autolearn=no autolearn_force=no version=3.4.4 Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rochester!udel!princeton!rutgers!mtune!codas!peora!petsd!cjh From: cjh@petsd.UUCP (Chris Henrich) Newsgroups: comp.lang.ada Subject: Re: HELP needed!! Message-ID: <1072@petsd.UUCP> Date: Tue, 14-Jul-87 19:41:51 EDT Article-I.D.: petsd.1072 Posted: Tue Jul 14 19:41:51 1987 Date-Received: Fri, 17-Jul-87 05:09:52 EDT References: <322@louie.udel.EDU> Reply-To: cjh@petsd.UUCP (C. J. Henrich) Organization: Perkin-Elmer DSG, Tinton Falls, N.J. Keywords: pointers, incomplete types List-Id: In article <322@louie.udel.EDU> klaiber@udel.EDU (Alexander Klaiber) writes: >HELP!!!!! >I am doing a high-level comparison of high-level languages and I've run >into a severe problem with Ada. What I am trying to do is the following: > >Assume I have a generic package linked_list that, given some type "item", >defines a type "list" which is a list of "item". > >I want to write a package that defined two types, A and B and operations on >these. Now my problem is that both A and B are records and A includes a >field of type "list of B" and B includes a field of type "list of A". I >want the lists to be handled by the generic linked_list. The following pattern is acceptable to at least one validated compiler (that of Concurrent Computer Corp.): PACKAGE my_package_1 IS TYPE a; -- "incomplete type declarations" TYPE b; TYPE a_ptr IS ACCESS a; -- about the only thing you can do with TYPE b_ptr IS ACCESS b; -- incomplete type declarations PACKAGE list_of_a_ptr IS NEW linked_list ( a_ptr ); PACKAGE list_of_b_ptr IS NEW linked_list ( b_ptr ); TYPE a IS RECORD xxx: list_of_b_ptr . list; END RECORD; TYPE b IS RECORD xxx: list_of_a_ptr . list; END RECORD; --- more declarations, including operations on types "a" and "b" END my_package_1; Note that your linked lists come out as lists of pointers. This design is open to criticism, because it makes both the record structure and the pointers visible to users of the package. There is some advantage to be gained from keeping the innards of "a" and "b" private. For one thing, increased freedom in using them to instantiate other generic units. For another, it allows you to separate the compilation of "a" and "b". Here is a scheme that is a little better from the viewpoint of software engineering: PACKAGE my_package_2_a IS TYPE abstract_a IS PRIVATE; -- fundamental operations with abstract_a PRIVATE TYPE record_a; TYPE abstract_a IS ACCESS record_a; END my_package_2_a; PACKAGE my_package_2_b IS TYPE abstract_b IS PRIVATE; -- fundamental operations with abstract_b PRIVATE TYPE record_b; TYPE abstract_b IS ACCESS record_b; END my_package_2_b; -- note how the implementation of "a" depends on the *specification* -- of "b" WITH my_package_2_b; USE my_package_2_b; WITH linked_list; PACKAGE BODY my_package_2_a IS PACKAGE linked_list_b IS NEW linked_list ( abstract_b ); TYPE record_a IS RECORD xxx: linked_list_b.list; END RECORD; -- lots of other implementation details END my_package_2_a; WITH my_package_2_a; USE my_package_2_a; WITH linked_list; PACKAGE BODY my_package_2_b IS PACKAGE linked_list_b IS NEW linked_list ( abstract_b ); TYPE record_b IS RECORD xxx: linked_list_b.list; END RECORD; -- list of other implementaiton details END my_package_2_b; Hope this helps. Regards, Chris -- Full-Name: Christopher J. Henrich UUCP: ...!hjuxa!petsd!cjh US Mail: MS 313; Concurrent Computer Corporation; 106 Apple St; Tinton Falls, NJ 07724 Phone: (201) 758-7288 Concurrent Computer Corporation is a Perkin-Elmer company.