comp.lang.ada
 help / color / mirror / Atom feed
* Generics and selection based on type
@ 2007-01-12 16:24 Maciej Sobczak
  2007-01-12 21:43 ` Björn Persson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Maciej Sobczak @ 2007-01-12 16:24 UTC (permalink / raw)


Hi,

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);

So that:

Int_To_String(7) = "7";
Positive_To_String(5) = "5";
Float_To_String(3.14) = "3.14";
Identity("Hello") = "Hello";

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?

-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generics and selection based on type
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Björn Persson @ 2007-01-12 21:43 UTC (permalink / raw)


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.

[...]

> 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?

I think that's impossible. You can only do things with the parameter that
you can do with all types that the generic function can be instantiated
with. I also don't see the point. Why don't you simply write a set of
overloaded functions? It won't take more work or more code, as you'll have
to write all the different implementations either way. Generics are for
cases where you can use the same implementation for different types.

-- 
Bj�rn Persson                              PGP key A88682FD
                   omb jor ers @sv ge.
                   r o.b n.p son eri nu



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generics and selection based on type
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey R. Carter @ 2007-01-12 21:50 UTC (permalink / raw)


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.
> 
> Is it possible?

No. You need different generic formal parameters for signed integer, 
modular integer, fixed-point, and floating-point types. See 
PragmARC.Images for an example:

http://pragmada.home.mchsi.com/

-- 
Jeff Carter
"Sir Robin the-not-quite-so-brave-as-Sir-Lancelot,
who had nearly fought the Dragon of Angnor,
who nearly stood up to the vicious Chicken of Bristol,
and who had personally wet himself at the
Battle of Badon Hill."
Monty Python & the Holy Grail
68



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generics and selection based on type
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2007-01-13  9:14 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-01-13  9:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox