From: Gene on
Hello friends,

I would like to do something like:

type Conditional_Result (Exists : Boolean := False) is
record case Exists is
when True =>
Value : Natural;
when False => null;
end record;

procedure Do_Something(x : in Natural; rtn : out Conditional_Result)
is
begin
if x = 0 then
rtn := (Exists => False);
else
rtn := (Exists => True; Value => x + 1);
end if;
end;

procedure Foo is
rtn : Conditional_Result;
begin
Do_Something(0, rtn);
if rtn.Exists then
Put_Line("Yes!");
end if;
Do_Something(1, rtn);
if rtn.Exists then
Put_Line("Yes!");
end if;
end;

ALRM 2005 and Barnes seem to say the procedure Do_Something will know
that rtn in foo is unconstrained and allow both calls to succeed. In
GNAT 2007, the second call fails with a constraint error at the
assignment to rtn. Can you tell me what I'm missing?

Thanks!
From: anon on
--
-- Except for a few typos it work just fine in Ada 95 / 2005
-- using GNAT 3.15p and GNAT 2007 and 2008.
--

with Ada.Text_IO ;
use Ada.Text_IO ;

procedure Foo is

type Conditional_Result (Exists : Boolean := False) is record
case Exists is
when True => Value : Natural;
when False => null;
end case ;
end record;


procedure Do_Something(x : in Natural; rtn : out Conditional_Result) is
begin
if x = 0 then
rtn := (Exists => False);
else
rtn := (Exists => True, Value => x + 1);
end if;
end;

rtn : Conditional_Result;


begin
Do_Something(0, rtn);
if rtn.Exists then
Put_Line("Yes!");
end if;

Do_Something(1, rtn);
if rtn.Exists then
Put_Line("Yes!");
-- added to test value result.
Put_Line("Value" & Integer'Image( rtn.Value) );
end if;

end;


In <c6f8a097-4675-4335-bbda-ae552be2e8e6(a)m73g2000hsh.googlegroups.com>, Gene <gene.ressler(a)gmail.com> writes:
>Hello friends,
>
>I would like to do something like:
>
>type Conditional_Result (Exists : Boolean := False) is
> record case Exists is
> when True =>
> Value : Natural;
> when False => null;
> end record;
>
>procedure Do_Something(x : in Natural; rtn : out Conditional_Result)
>is
>begin
> if x = 0 then
> rtn := (Exists => False);
> else
> rtn := (Exists => True; Value => x + 1);
> end if;
>end;
>
>procedure Foo is
> rtn : Conditional_Result;
>begin
> Do_Something(0, rtn);
> if rtn.Exists then
> Put_Line("Yes!");
> end if;
> Do_Something(1, rtn);
> if rtn.Exists then
> Put_Line("Yes!");
> end if;
>end;
>
>ALRM 2005 and Barnes seem to say the procedure Do_Something will know
>that rtn in foo is unconstrained and allow both calls to succeed. In
>GNAT 2007, the second call fails with a constraint error at the
>assignment to rtn. Can you tell me what I'm missing?
>
>Thanks!

From: Adam Beneschan on
On Jun 26, 5:22 pm, Gene <gene.ress...(a)gmail.com> wrote:
> Hello friends,
>
> I would like to do something like:
>
> type Conditional_Result (Exists : Boolean := False) is
> record case Exists is
> when True =>
> Value : Natural;
> when False => null;
> end record;
>
> procedure Do_Something(x : in Natural; rtn : out Conditional_Result)
> is
> begin
> if x = 0 then
> rtn := (Exists => False);
> else
> rtn := (Exists => True; Value => x + 1);
> end if;
> end;
>
> procedure Foo is
> rtn : Conditional_Result;
> begin
> Do_Something(0, rtn);
> if rtn.Exists then
> Put_Line("Yes!");
> end if;
> Do_Something(1, rtn);
> if rtn.Exists then
> Put_Line("Yes!");
> end if;
> end;
>
> ALRM 2005 and Barnes seem to say the procedure Do_Something will know
> that rtn in foo is unconstrained and allow both calls to succeed. In
> GNAT 2007, the second call fails with a constraint error at the
> assignment to rtn. Can you tell me what I'm missing?

It should work, and "anon" says he did get it to work with GNAT 2007.
But your example isn't a complete program, so perhaps if you post the
complete program you tried (assuming it's not much larger than this
example), maybe we can see something additional that you did that
would cause a problem.

-- Adam

From: Georg Bauhaus on
Gene wrote:

> ALRM 2005 and Barnes seem to say the procedure Do_Something will know
> that rtn in foo is unconstrained and allow both calls to succeed.

You can express your expectations in the program by adding

pragma Assert(not rtn'Constrained);


-- Georg
From: Dmitry A. Kazakov on
On Thu, 26 Jun 2008 17:22:29 -0700 (PDT), Gene wrote:

> type Conditional_Result (Exists : Boolean := False) is
[...]
> ALRM 2005 and Barnes seem to say the procedure Do_Something will know
> that rtn in foo is unconstrained and allow both calls to succeed.

Since you have a default value for the discriminant, it is perfectly OK to
assign it changing the constraint. This does not depend on wither the
parameter is in out or out. There shall be no Constraint_Error.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de