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,fa2cc518ef3b992c X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: tagged types extensions - language design question Date: 2000/01/29 Message-ID: #1/1 X-Deja-AN: 579024359 Content-transfer-encoding: 7bit References: Content-Type: text/plain; charset="US-ASCII" X-ELN-Date: Fri Jan 28 19:14:54 2000 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.prod.itd.earthlink.net 949115694 38.26.192.246 (Fri, 28 Jan 2000 19:14:54 PST) Organization: EarthLink Network, Inc. Mime-version: 1.0 NNTP-Posting-Date: Fri, 28 Jan 2000 19:14:54 PST Newsgroups: comp.lang.ada Date: 2000-01-29T00:00:00+00:00 List-Id: In article , "Vladimir Olensky" wrote: > Two classes not always equal to two packages. Yes, I think we agree on this. My example of this was the implementation of the Observer pattern. > Two classes could be in one file or in one enclosing class but two Ada > packages are different files for which I should invent names. Not necessarily. The relationship of packages ("program units") to files isn't specified by the language. Some compilers require that they be in separate files, but not all compilers do. If you do want to put multiple program units in a single file, then you can just use gnatchop to automatically create separate files for each unit. > Also if I have an abstract root type with private and I extend it to another > abstract tagged type with additional private fields and the last one is a > parent to two concrete types both of them having the same public attributes > then I should put that derived types in additional child package and duplicate > that public attributes in each derived type instead of putting them (public > attributes) in the abstract parent class. If I understand you correctly, I showed how to do this in my last message. You do not have to duplicate public attributes. The intermediate type in which the public attributes are declared can be shared among derived types, and without upsetting the "natural" package hierarchy. > Also I should duplicate primitive operations on that attributes for each > derived type instead of defining only one operation in the parent class > or even at upper level root class. The primitive operations for the intermediate type can be declared together with the declaration of the type. No duplication is necessary. > Or I should first create intermediate package in which public attributes are > added to the abstract parent (name it as Root.Parent.PubAttr) and then in > the final package to extend that abstract parent with private fields and then > in the same package I can create non-abstract derived classes. I could not > tell that it is very convenient. It would be better to have all this in one > package. I'm not sure what you're trying to say here, but it sounds similar the the example I posted in my last message. If you're trying to declare public and private attributes, then you're going to have to declare (abstract) intermediate types. Yes, this carries a syntactic burden, but we disagree about its severity. In any kind of design problem (here, the design of a computer language), one rule of thumb is "design for the common case." In other words, common things should be easy to do. Don't make the uncommon case easy, if that means making the common case harder. As Tucker pointed out, declaring an abstraction with both public and private attributes is not a common thing to do, and so they didn't design for that case specifically. This keeps the language simple. Yes, as a consequence, in this particular case you have to work a little harder in order to implement the abstraction, but you can get the job done. > class T { > protected: // visible only in the derived classes > int s ; > public: > int x,y; > T() { s =2; x=3; y=4;}; // constructor > }; package P is type T_Public is abstract tagged record X : Integer := 3; Y : Integer := 4; end record; type T is new T_Public with private; private type T is new T_Public with record S : Integer := 2; end record; end P; > class T1 : T { > protected: > int s1 ; > public: > int x1,y1; > int Calculate () > { > return (s+s1)*(x+x1+y+y1); > } > T1() { s1 =3; x1=6; y1=7;}; > }; package P.C is type T1_Public is abstract new T with record X1 : Integer := 6; Y1 : Integer := 7; end record; type T1 is new T1_Public with private; function Calculate (O : T1) return Integer; private type T1 is new T1_Public with record S1 : Integer := 3; end record; end P.C; > int main(int argc, char* argv[]) > { > int result; > T1 P; > result = P.Calculate(); > printf("result is %d \n", result); // result is 100 > return 0; > } with P.C; use P.C; procedure Main is O : T1; Result : constant Integer := Calculate (O); begin Put_Line ("Result is " & Integer'Image (Result)); end Main;