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.9 required=5.0 tests=BAYES_00, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,616091a85ff150f1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-12-05 14:45:20 PST Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada Subject: Re: Ada 200X Assertions Date: Wed, 5 Dec 2001 17:49:12 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <3C0C48BE.3B20F04E@adaworks.com> X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 X-Complaints-To: newsabuse@supernews.com Xref: archiver1.google.com comp.lang.ada:17483 Date: 2001-12-05T17:49:12-05:00 List-Id: "Mark Lundquist" wrote in message news:NNwP7.2247$dp6.159279@rwcrnsc54... > The mixin idiom takes a generic formal tagged type, and exports an extension > of that type with additional properties/behaviors. Well, you could argue there are two mixin idioms: the one you describe here, and another using access discriminants. If it's not clear to CLA readers, here are the two techniques: I. Generic Mixin generic type Parent_Type (<>) is abstract tagged limited private; package T_Mixin is type T is new Parent_Type with private; procedure Mixin_Op (O : in out T); private type T is new Parent_Type with ; end; Now you can add T_Mixin behavior to an existing tagged type: package P is type T is tagged limited private; ... end P; package Q is new T_Mixin (P.T); Now you use Q.T wherever type T_Mixin.T is expected: declare O : Q.T; begin Mixin_Op (O); end; However, I almost never do this. I prefer to use the access discriminant approach. II. Access discriminants package T_Mixin is type T is abstract tagged limited private; type T_Class_Access is access all T'Class; procedure Mixin_Op (O : access T) is abstract; private type T is abstract tagged limited null record; end; package P is type T is tagged limited private; ... end P; package Q is type NT is new P.T with private; function Mixin (O : access NT) return T_Mixin.T_Class_Access; private type Q_Mixin (O : access NT) is new T_Mixin.T with null record; procedure Mixin_Op (O : access Q_Mixin); type NT is new T with record Mixin : aliased Q_Mixin (NT'Access); end record; end Q; Now you can use Q.NT wherever type T_Mixin.T is expected: declare O : aliased Q.NT; begin Mixin_Op (Mixin (O'Access)); end; I've shown here what is essentially an Java-style interface. It doesn't have to be as ornate as this. Most of the time all you need is a way to mixin behavior into an existing type hierarchy. The most common reason for this is to add Controlled-ness to a type: package P is type T is tagged limited private; ... end P; package P.C is type NT is new T with private; private type Control_Type (O : access NT) is new Ada.Finalization.Limited_Controlled with null record; procedure Finalization (Control : in out Control_Type); type NT is new T with record Control : Control_Type (NT'Access); end record; end P.C; The type NT is a member of T'Class, but has "mixed in" Controlled behavior. Type T itself has nothing to do with type Controlled. The availability of this technique is the reason why Ada95 doesn't need multiple inheritance.