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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM,T_FILL_THIS_FORM_SHORT autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,9dcaa9f073e2ef8f X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news2.google.com!postnews.google.com!r27g2000prr.googlegroups.com!not-for-mail From: Adrian Hoe Newsgroups: comp.lang.ada Subject: Re: Type in allocator has deeper level than designated class-wide type Date: Mon, 13 Jun 2011 23:04:01 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <9bf2ef73-5cc7-4863-90d7-2e2cf3bcd294@o10g2000prn.googlegroups.com> <92fb5bf2-f43e-4ac9-97eb-6092f10e5607@e17g2000prj.googlegroups.com> NNTP-Posting-Host: 110.159.15.203 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1308031468 32661 127.0.0.1 (14 Jun 2011 06:04:28 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 14 Jun 2011 06:04:28 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r27g2000prr.googlegroups.com; posting-host=110.159.15.203; posting-account=coq9PAkAAAB2Xx46RZLFJw5dY9DVXW4- User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUARLECNK X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:20780 Date: 2011-06-13T23:04:01-07:00 List-Id: On Jun 14, 9:24=A0am, Adam Beneschan wrote: > On Jun 13, 6:07=A0pm, Adrian Hoe wrote: > > > I changed the line in procedure Add from > > > =A0 =A0 =A0 New_Element :=3D new Element_Record' > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ( Constructor.Init ( Na= me, Color, Data) ) ; > > > to > > > =A0 =A0 =A0 New_Element :=3D new Element_Record; > > > The compiler is still pointing the same warning to this line. > > > What exactly does the warning message "Type in allocator has deeper > > level than designated class-wide type" mean? Is the compiler unable to > > determine the size of the array during allocation? Or the array is a > > level deeper than the List.Access_Node? > > It has nothing to do with arrays. =A0When you declare a type as "access > all T'Class", an object of that type can't be made to point to a type > declared in a more deeply nested subprogram. =A0A simple example: > > package Pack1 is > =A0 =A0 type Root is tagged null record; > =A0 =A0 type Acc is access all Root'Class; > end Pack1; > > package Pack2 is > =A0 =A0 procedure Proc1; > end Pack2; > > with Pack1; > package body Pack2 is > =A0 =A0 procedure Proc1 is > =A0 =A0 =A0 =A0 type Child is new Pack1.Root with null record; > =A0 =A0 =A0 =A0 A : Pack1.Acc; > =A0 =A0 begin > =A0 =A0 =A0 =A0 A :=3D new Child; =A0 -- ILLEGAL > =A0 =A0 end Proc1; > end Pack2; > > This assignment statement is illegal, because somewhere later, A could > be copied to, say, another variable of type Pack1.Acc declared > globally in the body of Pack2, and then you'd have a pointer to a > Child that would exist after Proc1 has exited and the Child type no > longer exists, which is a big no-no. =A04.8(5.1) prohibits this. > > Since in this case, the record type (Element_Record) is declared in a > generic, that complicates things, because the generic *could* be > instantiated inside a procedure, which would lead to the same sort of > problem as above. =A0I don't remember all the rules, but depending on > where the type is declared and where the allocator occurs (generic > specification or body), it might be illegal to instantiate this > generic inside a procedure, or it might cause Program_Error to be > raised. =A0I just tried our compiler again and modified the source to > instantiate High_Cloud; if there's an instance of High_Cloud inside a > procedure, it *does* give a warning that Program_Error will be raised > (if the "new Element_Record" expression is reached). =A0If your compiler > gives the warning when the generic is first seen, before an instance > of the generic is seen, that's just a choice your compiler makes. =A0But > unless your program is arranged in a different order than I'm > guessing, or unless there are some new Ada 2012 rules that I'm not yet > familiar with, the compiler should not reject your generic, and it > should not reject it or cause Program_Error to be raised if there's an > instance of the generic that is *not* inside a subprogram. > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Ad= am I see and I followed this post by Robert Duff: https://groups.google.com/group/comp.lang.ada/browse_frm/thread/8fc030c3b6c= 79517/45208b566c079110?lnk=3Dgst&q=3Dgeneric+and+accessibility#45208b566c07= 9110 Hence, generic type Index is ( <> ) ; type Item is private; package High_Cloud is package List renames Heterogeneous_Doubly_Link_List; use List; type Element_Record is new List.Node with private; type Series_Access is new List.Access_List; private type Element_Record is new List.Node with record Name : Unbounded_String; Color : Unbounded_String; Data : Item; end record; ... end High_Cloud; In the package body of High_Cloud, I have a procedure: procedure Add ( Cloud : in out High_Cloud_Record; Name : in Unbounded_String; Color : in Unbounded_String; Data : in Item ) is New_Element : List.Access_Node; begin New_Element :=3D new Element_Record' ( Constructor.Init ( Name, Color, Data) ) ; Concatenate ( Cloud.Series, New_Element ) ; end Add; To use High_Cloud: with High_Cloud; package Hen is type I_Array is array ( 1 .. 31 ) of Integer; package G is new High_Cloud ( Positive, I_Array ) ; use G; end Hen; and in the procedure, I have: with High_Cloud; with Hen; procedure smalltest is package H renames Hen; use H; Data_Op : H.I_Array :=3D ( 3, 4, 2, 3, 3, 3, 0, 3, 6, 3, 2, 6, 8, 7, 3, 4, 3, 5, 1, 3, 1, 2, 2, 0, 2, 6, 8, 7, 3, 3, 3 ) ; begin H.G.Init ( Chart ) ; H.G.Add ( Chart, "OP", "#000000", Data_Op ) ; end smalltest; I don't know if this is the most elegant and effective way, but it works. -- Adrian Hoe http://adrianhoe.com/adrianhoe