From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,27dc164946031512 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon02.news.prodigy.net!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Limited_Controlled and constructor functions Date: Thu, 18 Jan 2007 11:41:25 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1169138486 9633 192.74.137.71 (18 Jan 2007 16:41:26 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 18 Jan 2007 16:41:26 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:Q8eVEt6tcuYQ5XjjfmvAFngtqcw= Xref: g2news2.google.com comp.lang.ada:8265 Date: 2007-01-18T11:41:25-05:00 List-Id: Maciej Sobczak 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