comp.lang.ada
 help / color / mirror / Atom feed
* Private constant of tagged limited controlled type
@ 2011-10-22  6:14 Malaise
  2011-10-22  6:29 ` Jeffrey Carter
  2011-10-23  6:18 ` Malaise
  0 siblings, 2 replies; 4+ messages in thread
From: Malaise @ 2011-10-22  6:14 UTC (permalink / raw)


Hello,
I have difficulties to initialise a private constant of a tagged type
that implements Limited_Controlled.
The following complains that "initialization of limited object
requires aggregate or function call":
with Ada.Finalization;
package Pack is
  type T is tagged limited private;
  C : constant T;
private
  type T is new Ada.Finalization.Limited_Controlled with record
    I : Integer := 0;
  end record;
  V : T;
  C : constant T := V;
end Pack;
Aggregate seems impossible because Limited_Controlled is abstract and
anyway T is very complex in fact.
A function (returning tagged type) has to be visible and anyway cannot
return V (in Ada 2005).

Is there any solution other than access type?
Thank you



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

* Re: Private constant of tagged limited controlled type
  2011-10-22  6:14 Private constant of tagged limited controlled type Malaise
@ 2011-10-22  6:29 ` Jeffrey Carter
  2011-10-22 15:16   ` Robert A Duff
  2011-10-23  6:18 ` Malaise
  1 sibling, 1 reply; 4+ messages in thread
From: Jeffrey Carter @ 2011-10-22  6:29 UTC (permalink / raw)


On 10/21/2011 11:14 PM, Malaise wrote:
> The following complains that "initialization of limited object
> requires aggregate or function call":

Right.

> with Ada.Finalization;
> package Pack is
>    type T is tagged limited private;
>    C : constant T;
> private
>    type T is new Ada.Finalization.Limited_Controlled with record
>      I : Integer := 0;
>    end record;
>    V : T;
>    C : constant T := V;
> end Pack;
> Aggregate seems impossible because Limited_Controlled is abstract and
> anyway T is very complex in fact.

I suggest you read ARM 4.3.1 and 4.3.2. You can write an aggregate of the form

(Ada.Finalization.Limited_Controlled with others => <>)

> A function (returning tagged type) has to be visible and anyway cannot
> return V (in Ada 2005).

This is an unusual claim. The extended return statement was introduced 
specifically for returning limited types from a function. You ought to read ARM 
6.5. You could write

return X : T;

to return a value of T with all components set to their default values.

-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall
44



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

* Re: Private constant of tagged limited controlled type
  2011-10-22  6:29 ` Jeffrey Carter
@ 2011-10-22 15:16   ` Robert A Duff
  0 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2011-10-22 15:16 UTC (permalink / raw)


Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes:

> On 10/21/2011 11:14 PM, Malaise wrote:
>> The following complains that "initialization of limited object
>> requires aggregate or function call":
>
> Right.
>
>> with Ada.Finalization;
>> package Pack is
>>    type T is tagged limited private;
>>    C : constant T;
>> private
>>    type T is new Ada.Finalization.Limited_Controlled with record
>>      I : Integer := 0;
>>    end record;
>>    V : T;
>>    C : constant T := V;

That's illegal, because you're trying to copy a limited object.
What if T had a self-referential component?
If the above were allowed, C.Self would incorrectly point
to V, not C.  What if T contained a protected object?
It makes no sense to copy locks.

>> end Pack;
>> Aggregate seems impossible because Limited_Controlled is abstract and
>> anyway T is very complex in fact.
>
> I suggest you read ARM 4.3.1 and 4.3.2. You can write an aggregate of the form
>
> (Ada.Finalization.Limited_Controlled with others => <>)

Right, or:

    C : constant T := (Ada.Finalization.Limited_Controlled with I => 123)

>> A function (returning tagged type) has to be visible and anyway cannot
>> return V (in Ada 2005).
>
> This is an unusual claim. The extended return statement was introduced
> specifically for returning limited types from a function. You ought to
> read ARM 6.5. You could write
>
> return X : T;

Right.  Or it could say

    return (Ada.Finalization.Limited_Controlled with others => <>);

But the comment, "cannot return V" is correct -- no matter whether
V is local or global.

> to return a value of T with all components set to their default values.

I also suggest reading this:

http://www.adacore.com/2007/09/24/ada-gem-10/

and also the following 2 gems, which I wrote to explain
how limited types work in Ada 2005.

- Bob



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

* Re: Private constant of tagged limited controlled type
  2011-10-22  6:14 Private constant of tagged limited controlled type Malaise
  2011-10-22  6:29 ` Jeffrey Carter
@ 2011-10-23  6:18 ` Malaise
  1 sibling, 0 replies; 4+ messages in thread
From: Malaise @ 2011-10-23  6:18 UTC (permalink / raw)


Thank you for the explanations.
My compiler (gnat) does not put reference to the ARM in the error
messages... this would indeed have help me to find the solutions on my
own.
Regards



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

end of thread, other threads:[~2011-10-23  6:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-22  6:14 Private constant of tagged limited controlled type Malaise
2011-10-22  6:29 ` Jeffrey Carter
2011-10-22 15:16   ` Robert A Duff
2011-10-23  6:18 ` Malaise

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