comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Can't access record attribute in derived type
Date: Wed, 8 Mar 2017 13:03:08 -0800 (PST)
Date: 2017-03-08T13:03:08-08:00	[thread overview]
Message-ID: <50e05b28-f260-472d-9736-343fa8439613@googlegroups.com> (raw)
In-Reply-To: <86mvcv4zyu.fsf@gaheris.avalon.lan>

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.

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 );
--...

    Type Creature is new Base_Creature with record
       Parents : Creature_Grouping.List;
    end record;

--...
    Type Person is new Creature with --[...]
    Function Father( Input : Person ) return Creature'Class is
      ( Input.Parents.First_Element );
--...
    Type Knight is new Person with --[...]

  parent reply	other threads:[~2017-03-08 21:03 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 [this message]
2017-03-08 21:16   ` Mart van de Wege
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox