* Constraints in extended return
@ 2011-03-19 10:42 Simon Wright
2011-03-19 13:30 ` Robert A Duff
2011-03-22 2:02 ` Randy Brukardt
0 siblings, 2 replies; 6+ messages in thread
From: Simon Wright @ 2011-03-19 10:42 UTC (permalink / raw)
This code transposes a matrix:
function Transpose (M : Complex_Matrix) return Complex_Matrix
is
begin
return Result : Complex_Matrix (M'Range (2), M'Range (1)) do
for J in M'Range (1) loop
for K in M'Range (2) loop
Result (K, J) := M (J, K);
end loop;
end loop;
end return;
end Transpose;
This is all very well for providing a value where no constraint is
otherwise imposed, for example as an actual in a subprogram call, but
what about the case where there is a prior constraint?
Input : Complex_Matrix (1 .. 2, 11 .. 12);
Output : Complex_Matrix (1 .. 2, 1 .. 2);
begin
Input := (...);
Output := Transpose (Input);
Is there any way for the extended return to determine the constraints of
the 'target'? I suspect not, but the language in RM6.5 is deep.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Constraints in extended return
2011-03-19 10:42 Constraints in extended return Simon Wright
@ 2011-03-19 13:30 ` Robert A Duff
2011-03-19 13:51 ` Dmitry A. Kazakov
2011-03-19 15:55 ` Simon Wright
2011-03-22 2:02 ` Randy Brukardt
1 sibling, 2 replies; 6+ messages in thread
From: Robert A Duff @ 2011-03-19 13:30 UTC (permalink / raw)
Simon Wright <simon@pushface.org> writes:
> This is all very well for providing a value where no constraint is
> otherwise imposed, for example as an actual in a subprogram call, but
> what about the case where there is a prior constraint?
You get Constraint_Error if the constraint is wrong.
But this isn't directly related to extended return statements.
You get the same thing for old-fashioned returns:
function F(...) return String is
begin
...
return "Hello";
end F;
X : String (1..100);
X := F(...); -- Constraint_Error
> Is there any way for the extended return to determine the constraints of
> the 'target'?
No. You can do that for 'out' parameters, but unfortunately not for
function results. So normally, you would avoid constraining at
the call site, and do things like:
Output : constant Complex_Matrix := Transpose (Input);
- Bob
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Constraints in extended return
2011-03-19 13:30 ` Robert A Duff
@ 2011-03-19 13:51 ` Dmitry A. Kazakov
2011-03-19 15:55 ` Simon Wright
1 sibling, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2011-03-19 13:51 UTC (permalink / raw)
On Sat, 19 Mar 2011 09:30:18 -0400, Robert A Duff wrote:
> Simon Wright <simon@pushface.org> writes:
>
>> Is there any way for the extended return to determine the constraints of
>> the 'target'?
>
> No. You can do that for 'out' parameters, but unfortunately not for
> function results.
Fortunately you mean, because how could we otherwise create unconstrained
objects?
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Constraints in extended return
2011-03-19 13:30 ` Robert A Duff
2011-03-19 13:51 ` Dmitry A. Kazakov
@ 2011-03-19 15:55 ` Simon Wright
1 sibling, 0 replies; 6+ messages in thread
From: Simon Wright @ 2011-03-19 15:55 UTC (permalink / raw)
Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> Simon Wright <simon@pushface.org> writes:
>
>> This is all very well for providing a value where no constraint is
>> otherwise imposed, for example as an actual in a subprogram call, but
>> what about the case where there is a prior constraint?
>
> You get Constraint_Error if the constraint is wrong.
> But this isn't directly related to extended return statements.
> You get the same thing for old-fashioned returns:
>
> function F(...) return String is
> begin
> ...
> return "Hello";
> end F;
>
> X : String (1..100);
>
> X := F(...); -- Constraint_Error
>
>> Is there any way for the extended return to determine the constraints of
>> the 'target'?
>
> No. You can do that for 'out' parameters, but unfortunately not for
> function results. So normally, you would avoid constraining at
> the call site, and do things like:
>
> Output : constant Complex_Matrix := Transpose (Input);
Thanks, that's what I thought would be the case. This will constrain my
solution :-) but we all need boundaries!
> - Bob
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Constraints in extended return
2011-03-19 10:42 Constraints in extended return Simon Wright
2011-03-19 13:30 ` Robert A Duff
@ 2011-03-22 2:02 ` Randy Brukardt
2011-03-22 8:28 ` Simon Wright
1 sibling, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2011-03-22 2:02 UTC (permalink / raw)
"Simon Wright" <simon@pushface.org> wrote in message
news:m2d3lngzmi.fsf@pushface.org...
> This code transposes a matrix:
>
> function Transpose (M : Complex_Matrix) return Complex_Matrix
> is
> begin
> return Result : Complex_Matrix (M'Range (2), M'Range (1)) do
> for J in M'Range (1) loop
> for K in M'Range (2) loop
> Result (K, J) := M (J, K);
> end loop;
> end loop;
> end return;
> end Transpose;
>
> This is all very well for providing a value where no constraint is
> otherwise imposed, for example as an actual in a subprogram call, but
> what about the case where there is a prior constraint?
>
> Input : Complex_Matrix (1 .. 2, 11 .. 12);
> Output : Complex_Matrix (1 .. 2, 1 .. 2);
> begin
> Input := (...);
> Output := Transpose (Input);
>
> Is there any way for the extended return to determine the constraints of
> the 'target'? I suspect not, but the language in RM6.5 is deep.
Why do we care what the bounds are? Assignments "slide" bounds; all that is
required is that the lengths of the array dimensions match. This is true
whether this is an extended return or a regular return (at least it had
better be; it would be awful to have the semantics change depending on the
form of the return statement).
For instance,
Output := Input;
is both a legal assignment and does not raise Constraint_Error (both
dimensions have length 2).
Now, if you get the lengths wrong, you have a problem, but not the actual
bounds.
Randy.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Constraints in extended return
2011-03-22 2:02 ` Randy Brukardt
@ 2011-03-22 8:28 ` Simon Wright
0 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 2011-03-22 8:28 UTC (permalink / raw)
"Randy Brukardt" <randy@rrsoftware.com> writes:
> "Simon Wright" <simon@pushface.org> wrote in message
> news:m2d3lngzmi.fsf@pushface.org...
>> This code transposes a matrix:
>>
>> function Transpose (M : Complex_Matrix) return Complex_Matrix
>> is
>> begin
>> return Result : Complex_Matrix (M'Range (2), M'Range (1)) do
>> for J in M'Range (1) loop
>> for K in M'Range (2) loop
>> Result (K, J) := M (J, K);
>> end loop;
>> end loop;
>> end return;
>> end Transpose;
>>
>> This is all very well for providing a value where no constraint is
>> otherwise imposed, for example as an actual in a subprogram call, but
>> what about the case where there is a prior constraint?
>>
>> Input : Complex_Matrix (1 .. 2, 11 .. 12);
>> Output : Complex_Matrix (1 .. 2, 1 .. 2);
>> begin
>> Input := (...);
>> Output := Transpose (Input);
>>
>> Is there any way for the extended return to determine the constraints of
>> the 'target'? I suspect not, but the language in RM6.5 is deep.
>
> Why do we care what the bounds are? Assignments "slide" bounds; all that is
> required is that the lengths of the array dimensions match. This is true
> whether this is an extended return or a regular return (at least it had
> better be; it would be awful to have the semantics change depending on the
> form of the return statement).
>
> For instance,
> Output := Input;
> is both a legal assignment and does not raise Constraint_Error (both
> dimensions have length 2).
>
> Now, if you get the lengths wrong, you have a problem, but not the actual
> bounds.
Thanks for that (which I can confirm works just fine with GNAT). Unable
to see the wood for the trees ...
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2011-03-22 8:28 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-19 10:42 Constraints in extended return Simon Wright
2011-03-19 13:30 ` Robert A Duff
2011-03-19 13:51 ` Dmitry A. Kazakov
2011-03-19 15:55 ` Simon Wright
2011-03-22 2:02 ` Randy Brukardt
2011-03-22 8:28 ` Simon Wright
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox