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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,d67ca8d922a33c8a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-03-01 08:48:20 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!sjc70.webusenet.com!news.usenetserver.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!local1.nntp.sjc.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 01 Mar 2004 10:48:16 -0600 Date: Mon, 01 Mar 2004 11:48:15 -0500 From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Heterogenous array References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: NNTP-Posting-Host: 24.147.77.160 X-Trace: sv3-9Y8mhtFkNqk4eNaoMb9kl73HXsKHmxJeNFl6oR8++7LEkQjMgmPAxooIBL5Jhr3JyG5fEEk2fd6fiyE!NXYUoYfF9c/7KO2lFqjkCgimTZyJMM+oiYcFHNrGcpkAiCysP4Wm+6Iu6S1ECQ== X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.1 Xref: archiver1.google.com comp.lang.ada:5978 Date: 2004-03-01T11:48:15-05:00 List-Id: Bj�rn Persson wrote: > I need an array type that can hold elements of different types. My > current attempt (somewhat simplified) is below. My question is, is there > a way to do this without pointers? Sure, try: with Ada.Strings.Bounded; with Ada.Strings.Unbounded; package Multirecord is package A_Bounded_String is new Ada.Strings.Bounded.Generic_Bounded_Length(80); subtype A_Bounded_String_Type is A_Bounded_String.Bounded_String; type T_Param_Type is (Int, Str); type Parameter_Definition(Param_Type : T_Param_Type := Int) is record Name : A_Bounded_String_Type; case Param_Type is when Int => Min : Integer; Max : Integer; Int_Value : Integer; when Str => Str_Value : Ada.Strings.Unbounded.Unbounded_String; end case; end record; type Parameter_Wrapper is record Content: Parameter_Definition; end record; type Parameter_Array is array(Positive range <>) of Parameter_Wrapper; end Multirecord; It should compile without problems and do exactly what you want. The type Parameter_Wrapper is needed so that the element type of Parameter_Array is constrained. The rule that requires this may seem silly but it is not. Objects of type Parameter_Definition can be different sizes, objects of type Parameter_Wrapper are not. In effect, the reason for having the Parameter_Wrapper type is for the compiler to have a hook to do the mutable type thing. (Which in Ada is only a property of records with (defaulted) discriminants but never of array elements.) Whether the implementation accomplishes this by making Parameter_Wrapper the size of the largest possible Parameter_Definition or uses "hidden" pointers is, of course, up to the implementation. Some compilers alway "allocate the max," others only do that if the maximum size is below some threshold. I am not sure whether there are any compilers that always use hidden pointers. -- Robert I. Eachus "The only thing necessary for the triumph of evil is for good men to do nothing." --Edmund Burke