comp.lang.ada
 help / color / mirror / Atom feed
From: dst17!mab@ford-wdl1.arpa  (Mark A Biggar)
Subject: Re: Ada Operators in 9x
Date: 28 Jun 93 23:37:20 GMT	[thread overview]
Message-ID: <1993Jun28.233720.14990@wdl.loral.com> (raw)

In article <1993Jun28.203652.5542@relay.nswc.navy.mil> bwallet@apssgi.nswc.navy
.mil (Brad Wallet) writes:
>an interesting little problem operators gave me lately:
>package MY_INTEGER is
>   subtype INTEGER_16 is private; --#  Defines a sixteen bit integer.
>   function "+"
>     (LEFT  : in INTEGER_16; --*     Left operand of the addition.
>      RIGHT : in INTEGER_16) --*     Right operand of the addition.
>     return INTEGER_16; --/  This function returns the sum of two sixteen bit 
integers.
>private
>   type INTEGER_16 is INTEGER range -32_767..32_767;
>   for INTEGER_16'size use 16;
>end MY_INTEGER;
>package body MY_INTEGER is
>   function "+"
>     (LEFT  : in INTEGER_16;
>      RIGHT : in INTEGER_16)
>     return INTEGER_16 is
>   begin
>      return (LEFT + RIGHT); -- (1)
>   end "+";
>end MY_INTEGER;

This recurses until you run out of stack memory.  By the overloading rules,
the only matching operation for "+" at (1) is the "+" you are currently
defining (which must be available because there is no other way to get 
recursion when you really DO want it.)   In order to get what you really
wanted you need to say:

	return (INTEGER_16(INTEGER(LEFT)+INTEGER(RIGHT));

or something similar.  Now the overloading rules make the "+" for
INTEGER the only usable version of "+".  Note the the conversion back
to INTEGER_16 will raise CONSTRAINT_ERROR on overflow, which is
probably what you want anyway.

--
Mark Biggar
mab@wdl.loral.com

             reply	other threads:[~1993-06-28 23:37 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-06-28 23:37 Mark A Biggar [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-07-09 16:51 Ada Operators in 9x Robert I. Eachus
1993-07-08 22:18 Laurence VanDolsen
1993-07-02 18:14 munck
1993-07-01 21:43 Dave Bashford
1993-07-01 15:39 Robert I. Eachus
1993-07-01 12:54 agate!howland.reston.ans.net!usenet.ins.cwru.edu!magnus.acs.ohio-state.ed
1993-07-01 10:28 agate!howland.reston.ans.net!darwin.sura.net!dtix.dt.navy.mil!relay-wo!re
1993-06-30 23:06 Robert I. Eachus
1993-06-30 17:41 agate!howland.reston.ans.net!darwin.sura.net!dtix.dt.navy.mil!relay-wo!re
1993-06-30 16:23 cis.ohio-state.edu!math.ohio-state.edu!darwin.sura.net!source.asset.com!v
1993-06-30 15:24 Charles H. Sampson
1993-06-28  7:13 ghost.dsi.unimi.it!univ-lyon1.fr!scsing.switch.ch!epflnews!disuns2.epfl.c
1993-06-27  8:26 Stephen J Bevan
1993-06-27  1:03 Kenneth Anderson
1993-06-26 15:43 cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!agate!doc.i
1993-06-26 15:19 cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!agate!doc.i
1993-06-25 21:12 Michael Feldman
1993-06-25 21:03 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
1993-06-25 20:56 Rahul Dhesi
1993-06-25 20:44 David Emery
1993-06-24 14:14 Joe Hildebrand
1993-06-22 13:01 cis.ohio-state.edu!math.ohio-state.edu!darwin.sura.net!cs.ucf.edu!longwoo
1993-06-21 22:14 Mike Berman
replies disabled

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