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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,bf5045b7cee3d4b X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feed.tweaknews.nl!83.80.28.12.MISMATCH!multikabel.net!newsfeed10.multikabel.net!news.astraweb.com!border1.a.newsrouter.astraweb.com!feeder.news-service.com!newsfeed.freenet.de!bolzen.all.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: tagged type as generic parameter Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Thu, 3 Jan 2008 16:51:24 +0100 Message-ID: NNTP-Posting-Date: 03 Jan 2008 16:51:27 CET NNTP-Posting-Host: 489f6304.newsspool2.arcor-online.net X-Trace: DXC=kLHS=@O=Hfa>jlK2>IgHGdA9EHlD;3Ycb4Fo<]lROoRa4nDHegD_]Re`nGUlYgR= On Thu, 03 Jan 2008 16:20:48 +0100, Philippe Tarroux wrote: > I wrote the following packages: > > generic > type Data is tagged private; -- In order to be able to build lists > of items of this type > package List is > > type List (<>) is private; > > private > type Item; > type Item_Ptr is access Item; > type Item is new Data with record -- Line 22 > Next : Item_Ptr; > end record; > > type List_Head is tagged record > Head : Item_Ptr := null; > end record; > > type List is access List_Head; > end List; > > ----------------------------------- > package data_handler is > > type Data is tagged private; > > function A_Data(V : Integer) return Data; > procedure Print (D : Data); > > private > > type Data is tagged record > Value : Integer; > end record; > > end data_handler; [...] > I suspect the problem is due to the derivation of Item from Data at line > 22 of list.ads during the instantiation of the generic but I don't > really understand why and i don't understand why the problem arises with > the function A_Data and not with the procedure Print. The function A_Data returns Data. It is a primitive operation of Data and thus covariant in the result. So when you derive anything from Data, you have to override it in order to provide a correct implementation that would return the derived type rather than the base. There are many ways to resolve the issue, the choice depends on other factors: 1. Class-wide A_Data: function A_Data (V : Integer) return Data'Class; This will be same (contravariant) for all descendants of A_Data and thus need not to overridden. 2. Data-specific List package: generic type Data is abstract new Data_Handler.Data with private; package Data_List is ... type Item is new Data with record Next : Item_Ptr; end record; function A_Data (V : Integer) return Item; We know here that Data is a descendant of Data_Handler.Data, so we can provide an override for A_Data. 3. Aggregation instead of inheritance: type Item is record Next : Item_Ptr; Value : Data; end record; No inheritance, no problem. 4. Non-generic implementation of lists. You can define a list interface and derive Data_Handler.Data from a list item. (In Ada 2005 it is easier to do than it was in Ada 95) That would reverse the inheritance order and thus solve the problem with A_Data. 5. Storage-pool based implementation of lists. You can design a storage pool that would maintain lists of allocated there items. In this case, unlikely to all other variants, the list item is not a fully separate type but just a pool-specific access to Data type. Because there again is no inheritance involved, the problem is solved. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de