comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: 'Digit is generic package
Date: 1996/08/24
Date: 1996-08-24T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.96Aug23204417@spectre.mitre.org> (raw)
In-Reply-To: 4vj3e6$hi1@goanna.cs.rmit.edu.au


In article <4vj3e6$hi1@goanna.cs.rmit.edu.au> ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe) writes:

  > Some time ago I posted a question but never saw a reply,
  > so here it is again, in somewhat less detail.

  > What I *want* to do is...

  >	       type Number is digits Max(Coord'Digits, Value'Digits);

  > (procedural solution deleted)
  > ...but it's a bit like a Johnson's "dog walking on its hind legs".

    One of the most easily forgotten features of Ada is that generic
instantiation happens at run-time.  This has a lot of advantages, but
there are some consequences that get ignored until they slap you in
the face.  This is one of them.  You can't generate code on most
machines which will operate on different numeric machine types.  So
Ada requires that choices of such types must occur at compile
time--sort of.  The compiler is expected to be able to reason that
several different are required for a single generic, and to call the
correct one.  However, for efficiency reasons, it is expected to be
possible to decide between versions for each source call at compile
time.  (The language is also defined so that all such calls can be
parameterized, but that is a different issue.)

    None of this prevents you from doing the dirty work under the
covers, and many math packages do.  The usual is to correctly
implement the algorithm for each machine type, and dispatch to the
correct (non-generic) version inside the generic:

    function Float_Float(X: in Float) return Float;
    function Long_Float_Float(X: in Long_Float) return Float;
    function Long_Float_Long_Float(X: in Long_Float) return Long_Float;
    ...

    generic
      type Foo is digits <>;
      type Bar is digits <>:
    function Generic_F(X: in Foo) return Bar is 
      case Foo'digits is
        when 1..6 => 
          case Bar'digits is
            when 1..6 => return Bar(Float_Float(Float(X)));
            when 7..13 => ...

    So yes, the pragmatic decision in building the language is that
single dispatch on sizes of machine types are comfortably handled
implicitly by generics.  Multiple dispatch works, but the default is
not right for all cases, and sometimes you have to roll your own.
Algorithms which depend on arithmetic functions of sizes definitely
fall in that category.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1996-08-24  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-08-23  0:00 'Digit is generic package Richard A. O'Keefe
1996-08-24  0:00 ` Robert I. Eachus [this message]
1996-08-29  0:00 ` Robert A Duff
replies disabled

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