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 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.albasani.net!.POSTED!not-for-mail From: peter.h.m.brooks@gmail.com (Peter Brooks) Newsgroups: comp.lang.ada Subject: Re: Variant record limitation - what's a better solution? Date: Tue, 9 Jul 2013 11:48:11 +0000 (UTC) Organization: albasani.net Message-ID: References: <0606a658-9816-4611-84dd-4f999bf6018e@googlegroups.com> <57dd4a15-f395-4938-89f2-027edadce24b@googlegroups.com> X-Trace: news.albasani.net TftFZ7nowq18LitNcM+Fb2MTit0mR4oSAPWXNBrHKVEl3w+NfRCn53WWvI993G84owCFRaBp2smSgWGz6lkHVmk9U0COyx4SbQJoIaeViCLciV2JUei9Wxzu7z3DC2xg NNTP-Posting-Date: Tue, 9 Jul 2013 11:48:11 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="mRnPjKe43V3QYJwId7sFBYsHFCFpO1eE8zVdZNEiWwtSBmmgESBsJNCeG6wu8L4fwx+9OnHy0/y0g3jOPxUE2OQ05iNhmhCnSn7HLO8dGkMRTDBomUfhelQuxb+NBhQV"; mail-complaints-to="abuse@albasani.net" User-Agent: tin/2.1.2-20121224 ("Langholm") (UNIX) (Darwin/12.4.0 (x86_64)) Cancel-Lock: sha1:+1KLHUHmuMxgEa7n6/nXf8jo+kU= Xref: news.eternal-september.org comp.lang.ada:16204 Date: 2013-07-09T11:48:11+00:00 List-Id: Adam Beneschan wrote: > 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? >> >> Here's an example: >> >> type >> my_object(X : size_type) is >> record >> name : string(1..80); >> case X is >> when small => Y : small_type; -- line 20 >> when medium => Y : medium_type; -- line 21 >> when large => Y: large_type; -- line 22 >> end case; >> end record; >> >> The errors are: >> line 21 'Y' conflicts with declaration at line 20 >> line 22 'Y' conflicts with declaration at line 21 >> >> I was hoping to have a different type depending on the case, but this doesn'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 tagged types instead of variant records. Variant records have been in Ada since Ada 83, but I think adding tagged types to Ada 95 has lessened (but not eliminated) the need to use variant records in many cases. The basic idea would 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_Object, you don't have to write them all as operations that do a CASE on a discriminant. 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 cases, you can define > > 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 object. > > 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 think should be considered, depending on your actual application. > Thank you for that idea. I think that that problably would make more sense for what I'm wanting to do.. I'm still not quite sure why a variant record can't have overloaded functions or entities, but I'm happy that it can't. Overriding the specific actions will work for me though.