help / color / mirror / Atom feed
* Valid attribute and input operations
@ 2023-09-23 20:22 Maciej Sobczak
  2023-09-23 21:48 ` Jeffrey R.Carter
  0 siblings, 1 reply; 5+ messages in thread
From: Maciej Sobczak @ 2023-09-23 20:22 UTC (permalink / raw)

Hi there,

I am in the middle of a heated debate with Richard Riehle on LinkedIn, where we cannot get to terms with regard to the exact semantics of X'Valid in the context of input operations performed by standard Get procedure.
In short, consider the following example:

with Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Is_Valid_Test is
   X : Integer range 0..200;
   Ada.Text_IO.Put("Get an Integer: ");
   if X'Valid then
      Ada.Text_IO.Put_Line("The Input is Valid ");
      Ada.Text_IO.Put_Line("The Input is not Valid ");
   end if;
end Is_Valid_Test;

When the input is 500, what should be the behavior of this program?
There are two interpretations:

1. Get is an input operation and can create invalid representations (as stated in 13.9.2, p.7). Then, the X'Valid test that follows Get(X) can be used to safely recognize whether the value is in the range or not. The program should print the second string (from the else branch), but should not raise any exceptions for this input (500).

2. Get is not an input operation in the meaning referred to in 13.9.2/7, or is an input, but only for type Integer (and it cannot create invalid integer representations on typical computers anyway). The X variable is an actual parameter that has a subtype that is different from the formal parameter and is subject to conversions when the Get subprogram exits normally (6.4.1/17,17a). This conversion should raise Constraint_Error for this input (500).

I have checked the above program on several on-line compilers, all of them behave according to interpretation 2 above.
Richard claims to get behavior 1 on his compiler.

What is your take on this? Any language lawyers?

Maciej Sobczak

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

end of thread, other threads:[~2023-09-28  3:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-23 20:22 Valid attribute and input operations Maciej Sobczak
2023-09-23 21:48 ` Jeffrey R.Carter
2023-09-26  6:13   ` Randy Brukardt
2023-09-26  7:22     ` Niklas Holsti
2023-09-28  3:27       ` Randy Brukardt

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