comp.lang.ada
 help / color / mirror / Atom feed
* Type of subtraction operator
@ 2009-05-28 21:23 Maciej Sobczak
  2009-05-28 21:54 ` Adam Beneschan
  2009-05-29  2:09 ` anon
  0 siblings, 2 replies; 5+ messages in thread
From: Maciej Sobczak @ 2009-05-28 21:23 UTC (permalink / raw)


According to 4.5.3/2, each numeric type has a subtraction operator
with this specification:

function "-"(Left, Right : T) return T

This means that the type of difference is the same as the type of
operands.

Which means that if T is Natural, then I should expect
CONSTRAINT_ERROR when the result of subtraction is less than 0.
But it does not happen:

with Ada.Text_IO;
procedure Test is

   X : Natural := 5;
   Y : Natural := 7;
   Z : Integer := X - Y;   -- here the result is not in Natural'Range

begin
   Ada.Text_IO.Put_Line (Integer'Image (Z));
end Test;

The above happily prints -2.
I thought initially that it is a subtype that is responsible for this,
but defining My_Natural type as a completely new type (with
appropriate type cast when Z is initialized) gives the same result.

I conclude that the effective type of difference (the return type of
the "-" operator) is T'Base instead of T.
Is that right? Is this a bug in AARM or did I misunderstand something?

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-05-30  7:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-28 21:23 Type of subtraction operator Maciej Sobczak
2009-05-28 21:54 ` Adam Beneschan
2009-05-29  2:09 ` anon
2009-05-29 15:24   ` Adam Beneschan
2009-05-30  7:47     ` anon

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