comp.lang.ada
 help / color / mirror / Atom feed
* Another question about controlled types.
@ 2008-02-09 20:57 Peter C. Chapin
  2008-02-09 21:50 ` Jeffrey R. Carter
  0 siblings, 1 reply; 3+ messages in thread
From: Peter C. Chapin @ 2008-02-09 20:57 UTC (permalink / raw)


I'm using GNAT GPL 2007. I understand that Ada 2005 allows one to return 
limited types from functions. Consider the following package 
specification and body:

package Check is
    type Some_Type is limited private;
    function Make(I : Integer) return Some_Type;
private
    type Some_Type is
       record
          Component : Integer;
       end record;
end Check;

package body Check is
    function Make(I : Integer) return Some_Type is
       Result : Some_Type;
    begin
       Result.Component := I;
       return Result;
    end Make;
end Check;

In fact, this compiles fine using "gnatmake -gnatc check.adb"

Now consider the following slight (but significant!) change to the 
specification:

with Ada.Finalization;

package Check is
    type Some_Type is limited private;
    function Make(I : Integer) return Some_Type;
private
    type Some_Type is new Ada.Finalization.Limited_Controlled with
       record
          Component : Integer;
       end record;
end Check;

In this case the full view of Some_Type is controlled. Now when I 
attempt to compile the package I get:

check.adb:8:14: (Ada 2005) cannot copy object of a limited type (RM-2005 
6.5(5.5/2))
check.adb:8:14: return by reference not permitted in Ada 2005
check.adb:8:14: consider switching to return of access type

I'm not sure why there is a problem now. I tried declaring Some_Type as 
tagged in the visible part of the package but that didn't help.

Peter



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

* Re: Another question about controlled types.
  2008-02-09 20:57 Another question about controlled types Peter C. Chapin
@ 2008-02-09 21:50 ` Jeffrey R. Carter
  2008-02-09 23:41   ` Peter C. Chapin
  0 siblings, 1 reply; 3+ messages in thread
From: Jeffrey R. Carter @ 2008-02-09 21:50 UTC (permalink / raw)


Peter C. Chapin wrote:
> I'm using GNAT GPL 2007. I understand that Ada 2005 allows one to return 
> limited types from functions. Consider the following package 
> specification and body:
> 
> package Check is
>    type Some_Type is limited private;
>    function Make(I : Integer) return Some_Type;
> private
>    type Some_Type is
>       record
>          Component : Integer;
>       end record;
> end Check;

The full view is not limited.

> with Ada.Finalization;
> 
> package Check is
>    type Some_Type is limited private;
>    function Make(I : Integer) return Some_Type;
> private
>    type Some_Type is new Ada.Finalization.Limited_Controlled with
>       record
>          Component : Integer;
>       end record;
> end Check;

Here the full view is limited. That is the difference.

To return a limited value, you now need to either return an aggregate or use the 
extended return statement:

return Some_Type'(Ada.Finalization.Limited_Controlled with Component => I);

or

return Result : Some_Type do
    Result.Component := I;
end return;

See ARM 6.5.

-- 
Jeff Carter
"I soiled my armor, I was so scared."
Monty Python & the Holy Grail
71



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

* Re: Another question about controlled types.
  2008-02-09 21:50 ` Jeffrey R. Carter
@ 2008-02-09 23:41   ` Peter C. Chapin
  0 siblings, 0 replies; 3+ messages in thread
From: Peter C. Chapin @ 2008-02-09 23:41 UTC (permalink / raw)


Jeffrey R. Carter wrote:


> Here the full view is limited. That is the difference.

Ah!

> To return a limited value, you now need to either return an aggregate or 
> use the extended return statement:
> 
> return Some_Type'(Ada.Finalization.Limited_Controlled with Component => I);
> 
> or
> 
> return Result : Some_Type do
>    Result.Component := I;
> end return;

Cool! I actually understand that. :-)

Thanks a lot.

Peter



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

end of thread, other threads:[~2008-02-09 23:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-09 20:57 Another question about controlled types Peter C. Chapin
2008-02-09 21:50 ` Jeffrey R. Carter
2008-02-09 23:41   ` Peter C. Chapin

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