From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fc8cef2534e95561 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: 'Digit is generic package Date: 1996/08/24 Message-ID: #1/1 X-Deja-AN: 176124505 references: <4vj3e6$hi1@goanna.cs.rmit.edu.au> organization: The Mitre Corp., Bedford, MA. newsgroups: comp.lang.ada Date: 1996-08-24T00:00:00+00:00 List-Id: 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...