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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,488864006f91ea7a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-06 20:34:03 PST Path: supernews.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!nntp-relay.ihug.net!ihug.co.nz!news-hog.berkeley.edu!ucberkeley!enews.sgi.com!newshub2.rdc1.sfba.home.com!news.home.com!news1.sttls1.wa.home.com.POSTED!not-for-mail From: "DuckE" Newsgroups: comp.lang.ada References: Subject: Re: Pascal's: Variant Record or C's union X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Message-ID: Date: Sat, 07 Apr 2001 03:29:17 GMT NNTP-Posting-Host: 24.6.221.63 X-Complaints-To: abuse@home.net X-Trace: news1.sttls1.wa.home.com 986614157 24.6.221.63 (Fri, 06 Apr 2001 20:29:17 PDT) NNTP-Posting-Date: Fri, 06 Apr 2001 20:29:17 PDT Organization: Excite@Home - The Leader in Broadband http://home.com/faster Xref: supernews.google.com comp.lang.ada:6602 Date: 2001-04-07T03:29:17+00:00 List-Id: In Ada, check out "Discriminants". In the reference section 3.8.1 of the LRM gives a couple of examples. Your record definition would look something like: type GOODS_TYPE is ( LIQUID, TEXTILE); type TYPE_GOODS( GOODS_TYPE_SELECTION : GOODS_TYPE := LIQUID ) is record ITEM_NAME : string( 1..6 ); case GOODS_TYPE_SELECTION is when LIQUID => LITRE : float; VOLUME_PCT : float; when TEXTILE => LENGTH : float; WIDTH : float; end case; end record; But you'll find that these records are less promiscuous than their counterpart in 'C' or Pascal. In Ada the following 3 type declarations have different meanings. A : TYPE_GOODS; B : TYPE_GOODS( LIQUID ); C : TYPE_GOODS( TEXTILE ); In the record definition for "TYPE_GOODS" a "default discriminant" is provided (that's the := LIQUID in the type declaration). Since their is a default discriminant, the declaration of "A" will by default use the LIQUID variant. Also, since the default is used in the declaration of "A", you may assign a new value to "A" from any record of type "TYPE_GOODS", whether it has LIQUID or TEXTILE variant. The declaration of "B" specifies the LIQUID variant is used. This variable can only contain values for the LIQUID variant form. The declaration of "C" specifies the TEXTILE variant is used. This variable can only contain values of the TEXTILE form. In Ada you may never assign a value to "GOODS_TYPE_SELECTION" except as part of a complete record assignment, and then only when the destination record is either of the same variant as the source of the assignment, or the destination is declared using the default descriminant. So for your assignments from your example: A := ( GOODS_TYPE_SELECTION => LIQUID, ITEM_NAME => 'COGNAC', LITRE => 0.7, VOLUME_PCT := 45 ); A := (GOODS_TYPE_SELECTION => TEXTILE, ITEM_NAME => 'RED ', LENGTH => 10.0, WIDTH => 0.6 ); This may sound a little bit involved, but it eliminates the case that occurs in both Pascal and 'C' where you can assign a value to a component of a record viewing the record as one variant type and retrieve the value viewing the record as a different variant. BTW: If that is what you're interested in, check out "Unchecked_Convesion" instead. I hope this helps, And I hope I got it right (that's my understanding anyway), I didnt' compile my examples, so please have mercy, SteveD "Frank" wrote in message news:UQqz6.6485$NR.552875@news3.oke.nextra.no... > Hi! > > How do I create a record in Ada, that behaves as the variant record in > Pascal or similar to the union in C? > It seems to me that the expression "variant" in Ada is used on how you > define?/declare? :-) types based on a "type with many possible > apperances":-). > > Something like: > > type TYPE_GOODS is > record > ITEM_NAME : string(1..6); > GOODS_TYPE : integer; > case GOODS_TYPE <--------??? > when LIQUID: <---------??? > LITRE : float; > VOLUME_PCT : float; > when TEXTILE: > LENGTH : float; > WIDTH : float; > end case; > end record; > > > A : TYPE_GOODS; > .. > .. > A.ITEM_NAME := 'COGNAC'; > A.GOODS_TYPE := LIQUID; > A.LITRE := 0.7; > A.VOLUME_PCT := 45; > > A.ITEM_NAME := 'RED '; > A.GOODS_TYPE := TEXTILE; > A.LENGTH := 10.0; > A.WIDTH := 0.6; > > > > > Frank > >