comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Generics???
Date: Mon, 02 Jul 2001 17:48:32 GMT
Date: 2001-07-02T17:48:32+00:00	[thread overview]
Message-ID: <3B40B485.F9E4074B@worldnet.att.net> (raw)
In-Reply-To: 3B40F6BA.63A5C0FB@ida.his.se

Short answer: You can't. It would be a violation of Ada's strong
typing.

Integer types and float types are not implicitly convertable from one
to the other. The problem is specifically the arithmetic operators for
each type, as well as some of the attributes of each type.

If you only want to compare values for equality, or assign one value
to another, then you can specify the generic formal parameter to be
private. If you want to perform arithmetic you must be specific about
whether the type is a floating point type, a fixed point type, an
integer type, or a modular type. Each type has its own definition of
the arithmetic operators. The LRM does not require compilers to
support ambiguity in the arithmetic operators for numeric types.

In a language such as C++ the compiler would simply
promote all integer types to doubles, perform the calculations, 
then (possibly) convert back to the template parameter's actual
type. This is possible because C++ allows implicit conversion of
all integer types to any floating point type. C++ also does not
have an equivalent to Ada attributes. Many attributes of floating
point types are not shared by integer types. 

Jim Rogers
Colorado Springs, Colorado USA

Michael Andersson wrote:
> 
> Hi!
> How do I specify that the generic parameter is a integer or a float. I
> know that you can use
> type T is range <> to specify that T is any Integer-type and that you
> can use type T is digit <> to specify that T is any type of float. But
> how can I use both at the same time?
> I've tried to use type T is private but then I can't use any of the
> +,-,*,/ operators.
> 
> Need your help, please!
> /Michael Andersson



  reply	other threads:[~2001-07-02 17:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-02 22:33 Generics??? Michael Andersson
2001-07-02 17:48 ` James Rogers [this message]
2001-07-02 18:54   ` Generics??? Ehud Lamm
  -- strict thread matches above, loose matches on Subject: below --
2001-07-02 18:42 Generics??? Beard, Frank
2001-07-03 10:32 ` Generics??? Anders Wirzenius
2001-07-03 17:31   ` Generics??? Stephen Leake
2001-07-03 19:09   ` Generics??? tmoran
2001-07-04  5:30     ` Generics??? Anders Wirzenius
1992-09-14 15:00 Generics? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!jvnc.net!net
1992-09-09  5:26 Generics? Andrew Dunstan
1992-09-09  1:34 Generics?  Rob Allen
replies disabled

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