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=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.224.42.141 with SMTP id s13mr3692848qae.3.1372879588577; Wed, 03 Jul 2013 12:26:28 -0700 (PDT) X-Received: by 10.50.108.47 with SMTP id hh15mr208537igb.12.1372879588367; Wed, 03 Jul 2013 12:26:28 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!mx05.eternal-september.org!feeder.eternal-september.org!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!news.glorb.com!t19no14159qam.0!news-out.google.com!f7ni806qai.0!nntp.google.com!t19no14377qam.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 3 Jul 2013 12:26:28 -0700 (PDT) In-Reply-To: <0606a658-9816-4611-84dd-4f999bf6018e@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ NNTP-Posting-Host: 66.126.103.122 References: <0606a658-9816-4611-84dd-4f999bf6018e@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <57dd4a15-f395-4938-89f2-027edadce24b@googlegroups.com> Subject: Re: Variant record limitation - what's a better solution? From: Adam Beneschan Injection-Date: Wed, 03 Jul 2013 19:26:28 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:16090 Date: 2013-07-03T12:26:28-07:00 List-Id: On Wednesday, July 3, 2013 12:52:12 AM UTC-7, Peter Brooks wrote: > I see that I can't do what I'd like to with a variant record. What should= I be doing? >=20 > Here's an example: >=20 > type > my_object(X : size_type) is > record > name : string(1..80); > case X is > when small =3D> Y : small_type; -- line 20 > when medium =3D> Y : medium_type; -- line 21 > when large =3D> Y: large_type; -- line 22 > end case; > end record; >=20 > The errors are: > line 21 'Y' conflicts with declaration at line 20 > line 22 'Y' conflicts with declaration at line 21 >=20 > I was hoping to have a different type depending on the case, but this doe= sn't seem allowed. What would achieve this? Peter, I think others have given adequate answers for how to change the example to= make it compile. One other thing you might want to consider is using tagg= ed types instead of variant records. Variant records have been in Ada sinc= e Ada 83, but I think adding tagged types to Ada 95 has lessened (but not e= liminated) the need to use variant records in many cases. The basic idea w= ould look something like this: type My_Object is abstract tagged record name : string (1..80); end record; type My_Object_Small is new My_Object with record Y : Small_Type; end record; type My_Object_Medium is new My_Object with record Y : Medium_Type; end record; type My_Object_Large is new My_Object with record Y : Large_Type; end record; The advantage is that for whatever operations you want to define for My_Obj= ect, you don't have to write them all as operations that do a CASE on a dis= criminant. Say you need an operation that dumps all the data in the object= ; instead of writing a big Dump procedure that handles all the possible cas= es, you can define=20 procedure Dump (Obj : My_Object) is abstract; and override it for each of the derived types: overriding procedure Dump (Obj : My_Object_Small); -- similarly for the other two where the body of each one is tailored to the data in that particular objec= t. =20 It's just something to consider. A variant record may be more appropriate = in some cases; and I think in some simpler cases, the tagged type solution = may be too heavy to be worth the effort. But it's a possibility that I thi= nk should be considered, depending on your actual application. -- Adam