comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin.dowie@btopenworld.com>
Subject: Re: Min/Max attribute makes promises it can't keep
Date: Tue, 27 Apr 2010 13:20:41 -0700 (PDT)
Date: 2010-04-27T13:20:41-07:00	[thread overview]
Message-ID: <9b17e781-f46a-4384-8809-b16602d10114@y30g2000yqh.googlegroups.com> (raw)
In-Reply-To: 98b7e6f2-32ed-465c-9a52-541b7878ca86@y17g2000yqd.googlegroups.com

On Apr 27, 8:34 pm, Alex Mentis <asmen...@gmail.com> wrote:
> I'm disappointed with some allowed syntax that seems a little error-
> prone.  Consider the following code:
>
> with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
>
> procedure Main is
>
>    Nat : constant Natural := 0;
>    Pos : Positive;
>
> begin
>
>    Get (Pos);
>    Put (Positive'Min(Nat, Pos)); -- Ada does not require the Min
> attribute to enforce a Positive result
>
> end Main;
>
> This program happily outputs that the minimum of (0 and whatever
> positive value you enter) is 0.  Now, I concede that the program is
> working exactly as the ARM specifies.  The Min (and Max) attribute
> functions accept and return types of S'Base, in this case
> Positive'Base.  But doesn't it seem like a bit of a tease to allow a
> programmer to specify S'Min if the compiler is allowed to ignore the
> type of S in the function's parameter list and the program does not
> raise a Constraint_Error at run-time if it returns a value outside the
> range of type S?
>
> If it's too hard to enforce strictly then maybe the functions should
> be named Unchecked_Min/Unchecked_Max.  Or maybe the programmer should
> be constrained to using the attributes with only a base type.  Or, at
> the very least, can't the compiler generate a warning about this?  I
> turned on all warnings in GPS and got nothing.
>
> Things that make you go hmmm...
>
> Alex

If you want the check, this should do:

begin
   Get (Pos);
   Put (Positive (Positive'Min(Nat, Pos)));
end ...

-- Martin



  reply	other threads:[~2010-04-27 20:20 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-27 19:34 Min/Max attribute makes promises it can't keep Alex Mentis
2010-04-27 20:20 ` Martin [this message]
2010-04-27 21:16   ` Robert A Duff
2010-04-27 22:46     ` Randy Brukardt
2010-04-28 10:36     ` Alex Mentis
2010-04-28 10:58       ` AdaMagica
2010-04-28 11:37         ` Gautier write-only
2010-04-28 11:47           ` AdaMagica
2010-04-28 13:28             ` Martin
2010-04-28 13:41             ` Dmitry A. Kazakov
2010-04-28 14:10               ` Georg Bauhaus
2010-04-28 14:53                 ` Dmitry A. Kazakov
2010-04-28 21:07                   ` Randy Brukardt
2010-04-28 22:17                     ` Dmitry A. Kazakov
2010-05-01  5:42                       ` Randy Brukardt
2010-05-01  6:28                         ` Dmitry A. Kazakov
2010-04-29  4:41                     ` AdaMagica
replies disabled

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