comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Generics and selection based on type
Date: Sat, 13 Jan 2007 10:14:09 +0100
Date: 2007-01-13T10:13:54+01:00	[thread overview]
Message-ID: <1r439f0dyhl9v.1wap1rga3t402$.dlg@40tude.net> (raw)
In-Reply-To: eo8coc$kjc$1@cernne03.cern.ch

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



      parent reply	other threads:[~2007-01-13  9:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-12 16:24 Generics and selection based on type Maciej Sobczak
2007-01-12 21:43 ` Björn Persson
2007-01-12 21:50 ` Jeffrey R. Carter
2007-01-13  9:14 ` Dmitry A. Kazakov [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox