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,5394d9ca5f955366 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: pointers & OOP Date: 1999/05/14 Message-ID: #1/1 X-Deja-AN: 477685916 References: <$DL10CAsSgL3Iwj3@jr-and-assoc.demon.co.uk> <7gn7gr$fr5$1@nnrp1.dejanews.com> <7gq27t$vnd$1@nnrp1.deja.com> NNTP-Posting-Date: Thu, 13 May 1999 21:35:09 PDT Newsgroups: comp.lang.ada Date: 1999-05-14T00:00:00+00:00 List-Id: John Robinson writes: > >> The use (in general) of one "major" type per package goes back to my > > > >Well, now its become one "major" type per package. This is a lot weaker > >than your original claim. I might even agree with this one, > > Yes - if I have a fault it is getting suckered into replying quickly and > not reviewing the words before sending (I actually thought I used the > word "major" or "primary" or similar in the original posting - but I > probably didn't). In any case, any implementation or secondary types I > use are not (in general) tagged types. One time you need to combine types in the same package is when you're using a factory method. A factory method takes an instance of one (tagged) type, and returns an instance of another (tagged) type. Something like: function Factory_Method (O : T1) return T2'Class; The operation is primitive for T1, and depends on types T1 and T2, therefore both types must be declared in the same package. Study the article Iterator and Factory Methods Combined, in the Nov 98 ACM patterns archive. In that example a stack has a factory method that returns an iterator. Below is the elided spec. There are two types in the same package, type Root_Stack and type Root_Iterator. Type Root_Stack has a factory method, Start_At_Top, that returns an instance of an iterator appropriate for iteration over that kind of stack. generic type Item_Type is private; package Stacks is type Root_Stack is abstract tagged limited null record; -- type T1 procedure Push (Item : in Item_Type; On : in out Root_Stack) is abstract; procedure Pop (Stack : in out Root_Stack) is abstract; ... type Root_Iterator is abstract tagged null record; -- type T2 function Start_At_Top (Stack : Root_Stack) return Root_Iterator'Class is abstract; -- the factory method function Is_Done (Iter : Root_Iterator) return Boolean is abstract; function Get_Item (Iter : Root_Iterator) return Item_Type is abstract; procedure Advance (Iter : in out Root_Iterator) is abstract; ... end Stacks; Declaring the data structure and its iterator together in the same package is a very natural idiom. The reason Booch didn't have two types per package (data structure type plus iterator type) in his component library is that he didn't have active iterators.