From: Mart van de Wege <mvdwege@gmail.com>
Subject: Re: Can't access record attribute in derived type
Date: Wed, 08 Mar 2017 22:16:45 +0100
Date: 2017-03-08T22:16:45+01:00 [thread overview]
Message-ID: <861su74gg2.fsf@gaheris.avalon.lan> (raw)
In-Reply-To: 50e05b28-f260-472d-9736-343fa8439613@googlegroups.com
Shark8 <onewingedshark@gmail.com> 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.
prev parent reply other threads:[~2017-03-08 21:16 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-08 14:15 Can't access record attribute in derived type Mart van de Wege
2017-03-08 14:24 ` G.B.
2017-03-08 15:06 ` Mart van de Wege
2017-03-08 19:30 ` Niklas Holsti
2017-03-08 21:05 ` Mart van de Wege
2017-03-08 20:08 ` Randy Brukardt
2017-03-08 21:06 ` Mart van de Wege
2017-03-08 21:12 ` Mart van de Wege
2017-03-08 21:25 ` Mart van de Wege
2017-03-08 21:50 ` Simon Wright
2017-03-08 22:35 ` Mart van de Wege
2017-03-08 21:03 ` Shark8
2017-03-08 21:16 ` Mart van de Wege [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox