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,81e608b9d333eaff X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nntpserver.com!zeus.nntpserver.com!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Generics and selection based on type 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: Sat, 13 Jan 2007 10:14:09 +0100 Message-ID: <1r439f0dyhl9v.1wap1rga3t402$.dlg@40tude.net> NNTP-Posting-Date: 13 Jan 2007 10:13:54 CET NNTP-Posting-Host: 673bca72.newsspool4.arcor-online.net X-Trace: DXC=m9E_h0[2]8]AX0F2i>bPFX_PDNcfSJ;bb[UIRnRBaCd On Fri, 12 Jan 2007 17:24:44 +0100, Maciej Sobczak wrote: > I would like to write a generic function that returns the string > representation of its parameter, which can be a number or a string. > The sketch is: > > generic > type T is ... > function To_String(X : T) return String; > > I want to use it this way: > > function Int_To_String is new To_String(Integer); > function Positive_To_String is new To_String(Positive); > function Float_To_String is new To_String(Float); > function Identity is new To_String(String); Why should they have different names? Ada has no problem with overloading on the result type. > For this, it would be good to be able to select the implementation of > To_String based on what the type is. If it's numeric - the 'Image > attribute can be used. If it's already String - just return the > argument. And so on. > > Is it possible? If that were possible, you wouldn't need any generics for that. Namely if T'Image were a primitive operation of T, rather than an attribute. Or at least, if it were defined on all types, as T'Write and T'Output are. That time (Ada 83), one didn't know how to do it right. (If you have T restricted to a narrower class of types, then it were possible, as Jeffrey has pointed out.) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de