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,6b6619eb9cada212 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: Help me to chose between ADA 95 and C++ Date: 1999/12/20 Message-ID: <83mf9n$p3b$1@nntp1.atl.mindspring.net>#1/1 X-Deja-AN: 563191636 References: <83b8il$i5k$1@nntp4.atl.mindspring.net> <3859abde_3@news1.prserv.net> <83dsno$5v2$1@nntp2.atl.mindspring.net> <385bab78_3@news1.prserv.net> Organization: MindSpring Enterprises X-Server-Date: 20 Dec 1999 23:49:11 GMT Newsgroups: comp.lang.ada Date: 1999-12-20T23:49:11+00:00 List-Id: In article <385bab78_3@news1.prserv.net>, "Matthew Heaney" wrote: >But you can add and even subtract operations from a scalar type: > > type Counter is new Integer; > > function "+" (L, R : Counter) return Counter is abstract; > > function "-" (L, R : Counter) return Counter is abstract; > >You can then use Counter'Succ and Counter'Pred. I am aware of this fact, Matthew. The example I posted has the benefit of 1) enforcing information hiding, 2) a set of operations with single, unambiguous usage, and 3) considerable simplicity. I suppose one could argue about this, but it would be ultimately a matter of opinion rather than a matter of technical superiority of one form over the other. > >You can also replace operations: > > type Heading_Type_Base delta 1.0/2**(-16) range -720.0 .. 720.0; > > subtype Heading_Type is > Heading_Type_Base range 0.0 .. Heading_Type_Base'Pred (360.0); > > function Predefined_Add > (L, R : Heading_Type_Base) return Heading_Type_Base renames "+"; > > function "+" (L, R : Heading_Type) return Heading_Type; > > function Predefined_Subtract > (L, R : Heading_Type_Base) return Heading_Type_Base renames "-"; > > function "-" (L, R : Heading_Type) return Heading_Type; > >Now you can implement "+" and "-" to have wrap-around behaviour: > > H := 359.0; > H := H + 2.0; -- H = 1.0 > H := 5.0; > H := H - 10.0; -- H = 355.0 > Again, this is not new information. It seems stylistically a little excessive, but not obscene. My original point was that, now and then it is useful to be able to declare a numeric type as private. Not always. Simply that, it is nice to have that capability available. > >But pre- and post-condition checking is a separate issue from overriding >predefined operators of a scalar type. We could declare your divide op >for a non-private type: > > type T is new Float; > > function Predefined_Divide (L, R : T) return T renames "/"; > > function "/" (R, L : T) return T; > > >You may prefer to not use an infix operator at all: > > type T is new Float; > > function Div (Dividend, Divisor : T) return T; > This does not behave exactly as the example I posted. Also, if I plan to export my own division, I don't want to also export the predefined version. This would be a case where I would want a private type to prevent anyone from using the predefined division. > >In any case you're free to raise whatever exceptions are appropriate. > Only if there is an opportunity to do so before actually using the predefined operators. That is exactly the point. > >As far as pre- and post-condition checking goes, I prefer to check using >pragma Assert. No, this is not an official standard, but it is a de >facto standard. (There's even an AI to add this pragma to the >language.) > pragma Assert is a nice addition to some compilers. As you note, it is not part of the standard and not implemented in all compilers. > >Agreed, but this doesn't mean you have to declare the type as private. > Maybe not, but declaring the type private ensures that I export only the operators relevant to the abstraction intended. It is not a matter of _have to_ , it is a matter of what is appropriate under a particular set of circumstances. Richard Riehle