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!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Mart van de Wege Newsgroups: comp.lang.ada Subject: Re: Can't access record attribute in derived type Date: Wed, 08 Mar 2017 22:16:45 +0100 Message-ID: <861su74gg2.fsf@gaheris.avalon.lan> References: <86mvcv4zyu.fsf@gaheris.avalon.lan> <50e05b28-f260-472d-9736-343fa8439613@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain X-Trace: individual.net joR6Tbt3OIp1LM2aqBT94AXyR2swbxB9fZIILMnRPhfjqf4O71 X-Orig-Path: gaheris.avalon.lan!not-for-mail Cancel-Lock: sha1:2T32BYW62u3LdgBSVDGi+sRldTU= sha1:gxEpgt//aE7mY7/JIyRT/vMCgs8= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Xref: news.eternal-september.org comp.lang.ada:33501 Date: 2017-03-08T22:16:45+01:00 List-Id: Shark8 writes: > On Wednesday, March 8, 2017 at 7:15:22 AM UTC-7, Mart van de Wege wrote: >> Hi, >> >> I have the following definitions >> >> private >> type Creature is new Base_Creature with record >> Attributes : Attribute_Array; >> Gender : Possible_Gender := Unknown; >> Current_Hit_Points : Integer; >> Parents : Group_Of_Creatures.List; >> -- An instance of Ada.Containers.Indefinite_Doubly_Linked_Lists >> end record; >> >> In child package #1 (Persons): >> >> private >> type Person is new Creature with record >> [...] >> and in child package #2 (Knights): >> type Knight is new Person with record >> [...] >> >> I try to read the first element of the Parents attribute in knights.adb >> like this: >> >> function Father (K : in Knight) return Knight is >> begin >> return K.Parents.First_Element; >> end Father; > > One Problem is that K.Parents.First_Element *isn't* of type Knight -- > It's of type Creature -- that is surely one problem. > Yeah, that is solved by some explicit conversions. I am not happy with that, but this is still the first phase of my attempt at implementing families, and getting my head around the intricacies of OO in Ada. > Secondly, the construct >> type Creature is new Base_Creature with record >> Attributes : Attribute_Array; >> Gender : Possible_Gender := Unknown; >> Current_Hit_Points : Integer; >> Parents : Group_Of_Creatures.List; >> -- An instance of Ada.Containers.Indefinite_Doubly_Linked_Lists >> end record; > cannot exist, as you cannot instantiate the Container without completing the type which can't be complete without the instantiation of the Container (because of the Parents element). > > I would recommend something like this: > > Type Base_Creature is abstract tagged record > Attributes : Attribute_Array; > Gender : Possible_Gender := Unknown; > Current_Hit_Points : Integer; > end record; > > --... > Package Creature_Grouping is new Ada.Containers.Indefinite_Doubly_Linked_Lists( Base_Creature'Class ); > --... That's more or less what I did. Creature is derived from abstract Base_Creature, and Group_Of_Creatures is defined as new Ada.Containers.Indefinite_Doubly_Linked_list (Element_Type => Base_Creature'Class). Mart -- "We will need a longer wall when the revolution comes." --- AJS, quoting an uncertain source.