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 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!postnews.google.com!v11g2000prk.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: Tue, 14 Jun 2011 01:01:13 -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 1308038521 3810 127.0.0.1 (14 Jun 2011 08:02:01 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 14 Jun 2011 08:02:01 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: v11g2000prk.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:20784 Date: 2011-06-14T01:01:13-07:00 List-Id: On Jun 14, 2:04=A0pm, Adrian Hoe wrote: > 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 ( = Name, 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 t= o > > > 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 compile= r > > 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. =A0Bu= t > > 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 -- = Adam > > I see and I followed this post by Robert Duff:https://groups.google.com/g= roup/comp.lang.ada/browse_frm/thread/8fc03... > > Hence, > > generic > =A0 =A0type Index is ( <> ) ; > =A0 =A0type Item is private; > package High_Cloud is > > =A0 =A0package List renames Heterogeneous_Doubly_Link_List; > =A0 =A0use List; > > =A0 =A0type Element_Record =A0 =A0 =A0 =A0 =A0 is new List.Node with priv= ate; > =A0 =A0type Series_Access =A0 =A0 =A0 =A0 =A0 =A0is new List.Access_List; > private > =A0 =A0type Element_Record is new List.Node with > =A0 =A0 =A0 record > =A0 =A0 =A0 =A0 =A0Name =A0 =A0 =A0 =A0 =A0 : Unbounded_String; > =A0 =A0 =A0 =A0 =A0Color =A0 =A0 =A0 =A0 =A0: Unbounded_String; > =A0 =A0 =A0 =A0 =A0Data =A0 =A0 =A0 =A0 =A0 : Item; > =A0 =A0 =A0 end record; > ... > end High_Cloud; > In the package body of High_Cloud, I have a procedure: > =A0 =A0procedure Add > =A0 =A0 =A0 ( Cloud =A0 =A0 =A0 =A0 =A0: in out High_Cloud_Record; > =A0 =A0 =A0 =A0 Name =A0 =A0 =A0 =A0 =A0 =A0: in =A0 =A0 Unbounded_String= ; > =A0 =A0 =A0 =A0 Color =A0 =A0 =A0 =A0 =A0 : in =A0 =A0 Unbounded_String; > =A0 =A0 =A0 =A0 Data =A0 =A0 =A0 =A0 =A0 =A0: in =A0 =A0 Item ) > =A0 =A0is > =A0 =A0 =A0 New_Element : List.Access_Node; > =A0 =A0begin > =A0 =A0 =A0 New_Element :=3D new Element_Record' > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ( Constructor.Init ( Name= , Color, Data) ) ; > =A0 =A0 =A0 Concatenate ( Cloud.Series, New_Element ) ; > =A0 =A0end Add; > > To use High_Cloud: > > with High_Cloud; > > package Hen is > > =A0 =A0type I_Array is array ( 1 .. 31 ) of Integer; > =A0 =A0package G is new High_Cloud ( Positive, I_Array ) ; > =A0 =A0use G; > > end Hen; > > and in the procedure, I have: > > with High_Cloud; > with Hen; > > procedure smalltest is > > =A0 =A0package H renames Hen; > =A0 =A0use H; > > =A0 =A0Data_Op : H.I_Array :=3D ( 3, 4, 2, 3, 3, 3, 0, 3, 6, 3, 2, 6, 8, = 7, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 3, 4, 3, 5, 1, 3,= 1, 2, 2, 0, 2, 6, 8, 7, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 3, 3, 3 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ) ; > begin > > =A0 =A0H.G.Init ( Chart ) ; > =A0 =A0H.G.Add =A0( Chart, "OP", "#000000", Data_Op ) ; > > end smalltest; > > I don't know if this is the most elegant and effective way, but it > works. > -- > Adrian Hoehttp://adrianhoe.com/adrianhoe But this defeats my implementation intention which the size and the type of its data array can only be determined during runtime. Any suggestion? Thanks. -- Adrian Hoe http://adrianhoe.com/adrianhoe