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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fba93c19bb4e7dbd X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-15 00:12:15 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newspeer.monmouth.com!news.monmouth.com!shell.monmouth.com!not-for-mail From: ka@sorry.no.email (Kenneth Almquist) Newsgroups: comp.lang.ada Subject: Re: Q: Endless loop by dispatching Date: 15 Jul 2003 03:11:51 -0400 Organization: A poorly-installed InterNetNews site Message-ID: References: <3F0FC8C7.4090105@snafu.de> NNTP-Posting-Host: shell.monmouth.com Xref: archiver1.google.com comp.lang.ada:40280 Date: 2003-07-15T03:11:51-04:00 List-Id: Michael Erdmann wrote: > Thanks. In fact i have now selected this aproach. Each package > derives from the base type (A.Object) is forced to implement > a procedure Serialize by definint it in A as abstract. > > The bad thing about it, is that i wanted to add some code > which is common to all implementation of A.Object in the > Central Serialize procedure (e.g. writing the attrbiute > name if ront of every field of B.Object) Now i have to duplicate > this code in all implementations of A.Object. The way to avoid duplicate code is to place the code in a generic procedure which is instantiated for each type. Something along the line of: package A is type Object is tagged private; -- Write out all of the attributes of an object. procedure Serialize(This : Object); -- Write the value of a single attribute. procedure Write(This : Object; Attribute : Attribute_Id); -- Serialize_Extension for type T writes the attributes -- which appear in T type but not in the parent of type T. generic type T is new Object with private; Attributes : in Attribute_Array; with procedure Write(This : Object; Attribute : Integer) is <>; procedure Serialize_Extension(This : Object); end A; package B is type Object is new A.Object with private; -- Write out all of the attributes of an object. procedure Serialize(This : Object); -- Write the value of a single attribute. procedure Write(This : Object; Attribute : Attribute_Id); end B; package body B is My_Attributes : constant Attribute_Array := (...); procedure Serialize(This : Object) is procedure SE is new Serialize_Extension(Object, My_Attributes); begin Serialize(A.Object(This)); SE(This); end Serialize; procedure Write(This : Object; Attribute : Attribute_Id) is ...; end B; Kenneth Almquist