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-7-bit X-Google-Thread: 103376,2f63c66b1f04412d X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-11-17 18:48:55 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.mathworks.com!wn13feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!attbi_s04.POSTED!not-for-mail From: "Steve" Newsgroups: comp.lang.ada References: Subject: Re: How to define an array of a variant record type? X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: NNTP-Posting-Host: 12.211.58.135 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s04 1069123734 12.211.58.135 (Tue, 18 Nov 2003 02:48:54 GMT) NNTP-Posting-Date: Tue, 18 Nov 2003 02:48:54 GMT Organization: Comcast Online Date: Tue, 18 Nov 2003 02:48:54 GMT Xref: archiver1.google.com comp.lang.ada:2605 Date: 2003-11-18T02:48:54+00:00 List-Id: To do this you must use a "default discriminant". In your record type definition use: type Name_Value( Value_Type : Name_Value_Type := String_Type ) is ... There are a couple of other things you should be aware of. 1. When assigning values to the record, you can only change the type by doing a complete record assignment. That is: Value : Name_Value; Value := ( Value_Type => String_Type, --this is ok String_Value => To_Unbounded_String( "Hello" ); Value.Value_Type := Integer_Type; -- will not work Value := ( Value_Type => Integer_Type, -- ok Integer_Value => 42 ); Value.Integer_Value := 43; -- ok 2. You can only change the discriminant through the record assignment if the record was declared using the default. That is: Value : Name_Value(Integer_Type); Value := ( Value_Type => String_Type, -- will not work. String_Value => To_Unbounded_String( "Hello" ); Steve (The Duck) "Harald Schmidt" wrote in message news:BBDEBC6C.902D%office@anobject.net... > Hi, > > I would like to define an array, which its element type is a variant record > and the Value_Type is only known at runtime. > > type Name_Value_Type is (String_Type, > Integer_Type, > Float_Type); > > type Name_Value(Value_Type : Name_Value_Type) is > record > The_Name : Unbounded_String; > case Value_Type is > when String_Type => > String_Value : Unbounded_String; > when Integer_Type => > Integer_Value : Integer; > when Float_Type => > The_Value : Float; > end case; > end record; > > type Name_Value_Sequence is array(Natural range <>) of Name_Value; > > The compiler says "unconstrained element type in array declaration", > ...right! > > I know Ada is a strong typed language, but sometimes information is only at > runtime available. Does anyone know some workaround, or a regulare design > pattern for this problem? > > Harald > > >