comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Limited_Controlled and constructor functions
Date: Thu, 18 Jan 2007 11:41:25 -0500
Date: 2007-01-18T11:41:25-05:00	[thread overview]
Message-ID: <wcc3b6870ei.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: eonfu7$71m$1@cernne03.cern.ch

Maciej Sobczak <no.spam@no.spam.com> writes:

> Hi,
>
> Consider another Limited_Controlled problem:
>
> -- p.ads:
> with Ada.Finalization;
> package P is
>    type T (<>) is private;
>    function Constructor(I : Integer) return T;
> private
>    type Array_Of_Ints is array (Positive range <>) of Integer;
>    type T (Size : Positive) is new Ada.Finalization.Controlled with
>       record
>          A : Array_Of_Ints(1..Size);
>       end record;
> end P;
>
> -- p.adb:
> package body P is
>    function Constructor(I : Integer) return T is
>    begin
>       return T'(Ada.Finalization.Controlled with
>                 Size => 1, A => (1 => I));
>    end Constructor;
> end P;
>
> It looks a bit convoluted. :-)
> The idea is to have a T type that contains the array of integers and a
> constructor function that creates T with single element in the contained
> array. T is private, controlled and indefinite, to force the use of
> constructor function whenever T is declared.
>
> Now, I want to make it limited as well by adding limited before private
> and changing Controlled to Limited_Controlled in two places.
> I get this when compiling the package:
>
> cannot return a local value by reference
> "Program_Error" will be raised at run time
>
> What's going on?

It works in Ada 2005.  It's one of my favorite features of Ada 2005.
(By the way, I did part of the work of implementing this in GNAT.  The
way it works internally is "interesting", because it is important that
the above "return" not make a copy; it must build the new object in its
final resting place.)

Limited types were somewhat broken in Ada 83.  We tried to fix them in
Ada 95, but the "return by reference" idea was another mistake.
That's fixed in Ada 2005.

I'm not entirely sure what you're trying to accomplish, since the length
of that array can't change, and there's nothing interesting for Finalize
to do in this example.  Maybe you have other constructors?

Anyway, you can do things like this:

with Ada.Finalization;
package P is
   type T (Length : Natural; Initial : Integer) is private;
   function Constructor(I : Integer) return T;
private
   type Array_Of_Ints is array (Positive range <>) of Integer;
   type T (Length : Natural; Initial : Integer) is new Ada.Finalization.Limited_Controlled with
      record
         A : Array_Of_Ints(1..Length) := (others => Initial);
      end record;
end P;

My_Object : P.T (Length => 1, Initial => 123);

but I'm not sure if that's what you want.  The point is, in Ada 95,
limited types cannot have constructor functions, which is annoying,
but you can often work around the problem by passing information in
as discriminants.

- Bob



  parent reply	other threads:[~2007-01-18 16:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-18  9:51 Limited_Controlled and constructor functions Maciej Sobczak
2007-01-18 12:13 ` AW: " Grein, Christoph (Fa. ESG)
2007-01-18 16:41 ` Robert A Duff [this message]
2007-01-19  7:58   ` Maciej Sobczak
2007-01-19  9:41     ` Dmitry A. Kazakov
2007-01-19 13:45       ` Maciej Sobczak
2007-01-19 14:33         ` Dmitry A. Kazakov
2007-01-22  8:59           ` Maciej Sobczak
2007-01-20 17:09     ` Gautier
2007-01-20 19:39       ` Gautier
replies disabled

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