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.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,18684ed750c9b60f X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.180.101.9 with SMTP id fc9mr3123683wib.3.1350352570890; Mon, 15 Oct 2012 18:56:10 -0700 (PDT) Path: q10ni65138175wif.0!nntp.google.com!feeder2.cambriumusenet.nl!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!news.panservice.it!feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Fri, 12 Oct 2012 01:52:38 +0200 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to get generic formal parameter type into base class References: <51f8461d-d362-4e5f-a188-ac96a699a211@googlegroups.com> <527f8487-a578-406c-86c3-b3b0596cda71@l32g2000yqb.googlegroups.com> In-Reply-To: Message-ID: <50775bc6$0$6560$9b4e6d93@newsspool4.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 12 Oct 2012 01:52:38 CEST NNTP-Posting-Host: 103e7bc7.newsspool4.arcor-online.net X-Trace: DXC=eL8TiB>JdnY1`E>oC;JXEZ4IUKejVXUSnVcf8i= On 11.10.12 22:25, kevin andrew wrote: > FYI, you can do this with C++ template classes, and though I have > never tried, Java has added templates as well. Like the C++ version, this establishes the required relationship between Int and Char; C++'s protected visibility is achieved with the help of a child package. package Typedef is subtype Value_T is Integer; end Typedef; with Typedef; use Typedef; generic type T is new Value_T; package BaseClass is type BaseClass is tagged private; procedure Decode (Item : in out BaseClass; Value: Value_T) is null; function GetValue (Item : BaseClass) return T; private type BaseClass is tagged record Data : T; Value : Value_T; end record; end BaseClass; package body Baseclass is Default : T; function GetValue (Item : BaseClass) return T is begin return Default; end GetValue; end Baseclass; with Typedef; use Typedef; generic type T is new Value_T; package BaseClass.NewClass is type NewClass is new BaseClass with private; overriding procedure Decode (Item : in out NewClass; Value: Value_T); overriding function GetValue (Item : NewClass) return Standard.BaseClass.T; private type NewClass is new BaseClass with null record; end BaseClass.NewClass; package body BaseClass.NewClass is overriding procedure Decode (Item : in out NewClass; Value: Value_T) is begin Item.Value := Value; Item.Data := STandard.BaseClass.T(Value); end Decode; overriding function GetValue (Item : NewClass) return Standard.BaseClass.T is begin return Item.Data; end Getvalue; end BaseClass.NewClass; with Typedef; use Typedef; with BaseClass.NewClass; with Ada.Text_IO; with Ada.Integer_Text_IO; procedure main is subtype Int is Integer range Integer'Range; subtype Char is Integer range -128 .. 127; package BaseClassInstance is new BaseClass(Int); package NewClassInt is new BaseClassInstance.NewClass(Int); IntClass: NewClassInt.NewClass; package BaseClassInstance2 is new BaseClass(Char); package NewClassChar is new BaseClassInstance2.NewClass(Char); CharClass: NewClassChar.NewClass; Value : Value_T := 4; begin IntClass.Decode(Value); declare I : Integer := IntClass.GetValue; begin Value := 97; CharClass.Decode(Value); declare Ch: Char := CharClass.GetValue; use Ada.Text_IO, Ada.Integer_Text_IO; begin Default_Width := 0; Put ("int="); Put(I); Put(" ch="); Put (Character'Val(Ch)); New_Line; end; end; end Main;