comp.lang.ada
 help / color / mirror / Atom feed
* Min/Max attribute makes promises it can't keep
@ 2010-04-27 19:34 Alex Mentis
  2010-04-27 20:20 ` Martin
  0 siblings, 1 reply; 17+ messages in thread
From: Alex Mentis @ 2010-04-27 19:34 UTC (permalink / raw)


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



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

end of thread, other threads:[~2010-05-01  6:28 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-27 19:34 Min/Max attribute makes promises it can't keep Alex Mentis
2010-04-27 20:20 ` Martin
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

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