comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison <dennison@telepath.com>
Subject: Re: Source code preprocessing ?
Date: Fri, 26 Jan 2001 19:40:57 GMT
Date: 2001-01-26T19:40:57+00:00	[thread overview]
Message-ID: <94sjs6$9en$1@nnrp1.deja.com> (raw)
In-Reply-To: 3A71B72E.5BC97054@loria.fr

In article <3A71B72E.5BC97054@loria.fr>,
  Xavier HILAIRE <xavier.hilaire@loria.fr> wrote:

> Thank you for your answer. Meantime, I realized the problem of porting
> my #ifdefs was readily solved in Ada thanks to genericity. As a single
> example,
> in C++ I have :
>
> #ifdef WANT_ABRITARY_PRECISION
>
> typedef cl_R Rational;
> typedef cl_I Integer;
>
> #else
>
> typedef long long double Rational;
> typedef long long int Integer;
>
> #endif

You are talking about passing the actual type of Rational into a generic
as an instantiation parameter, right?

I don't think that really solves your problem, so much as it moves it
around a little. You still have to have some code somewhere that
instantiates that type with "cl_R" or "long long double" depending on
some condition. Sure, you could put that in a source file with a version
for each platform (to be chosen by the build system). But then what have
you gained over just doing that for the type without using the generic?

> I presume using a generic type in Ada is not enough, because one can
> never know if equality between elements makes sense or not, especially
> the compiler. But using a generic package *and* a boolean value
> indicating whether or not equality on rationals make sense or not
> should solve it (am I right ?)

First off, in Ada "=" (and ":=") exists for every type (excepting those
defined as "limited"). This includes records and arrays. Ada is a *real*
language, not some toy like C. :-)

Secondly, in a generic this issue is typically handled by one of the
following methods:

If the actual type to be supplied is unknown in composition, but can be
a record or scalar (or private type), then the formal parameter should
be defined as "private". This means that the generic can assume that "="
is available (among other things).

If the actual type to be supplied could be a "limited" type (for which
"=" is *not* available), then the formal should be defined as "limited
private". This means that the "=" function will not be available
(assignment won't be available either).

If "=" must be available, but the user might want to use a custom
equality (for instance, you might want to dereference a pointer in the
object and compare the contents rather than the pointers), then a formal
parameter for the "=" function for that type may be added to the
generic's formal parameters. The instantiating code can supply any
function with the same parameter profile as the actual and it will be
used for "=" within the generic.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



      reply	other threads:[~2001-01-26 19:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <3A6D87AD.66EE0AF@loria.fr>
     [not found] ` <94k90a$v1n$1@nnrp1.deja.com>
2001-01-26 17:43   ` Source code preprocessing ? Xavier HILAIRE
2001-01-26 19:40     ` Ted Dennison [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