comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Type of subtraction operator
Date: Thu, 28 May 2009 14:23:52 -0700 (PDT)
Date: 2009-05-28T14:23:52-07:00	[thread overview]
Message-ID: <8ae800c6-4307-4dc5-bf6b-d97101ae8521@x5g2000yqk.googlegroups.com> (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



             reply	other threads:[~2009-05-28 21:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-28 21:23 Maciej Sobczak [this message]
2009-05-28 21:54 ` Type of subtraction operator Adam Beneschan
2009-05-29  2:09 ` anon
2009-05-29 15:24   ` Adam Beneschan
2009-05-30  7:47     ` anon
replies disabled

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