comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Confused about class-wide types
Date: Sun, 20 Mar 2016 13:29:45 +0100
Date: 2016-03-20T13:29:45+01:00	[thread overview]
Message-ID: <ncm547$o7p$1@gioia.aioe.org> (raw)
In-Reply-To: 86oaa97635.fsf@gaheris.avalon.lan

On 2016-03-20 12:15, Mart van de Wege wrote:

> I'm finding myself a little confused about the interaction between
> class-wide types.
>
> Here's some example code:
>
> Spec:
>
> with Ada.Containers.Vectors;
> with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
> package Types_Test is
>     type Event is abstract tagged null record;
>     type Event_Ptr is access Event'Class;
>     type Message_Event is new Event with private;
>     type Message_Event_Ptr is access Message_Event'Class;
>     procedure Message (M : in out Message_Event_Ptr;
> 		      Mess : in String);

    function Message (Mess : in String) return Event_Ptr;

>     package Events is new Ada.Containers.Vectors ( Index_Type => Natural,
> 						 Element_Type => Event_Ptr);
> private
>     type Message_Event is new Event with record
>        Message : Unbounded_String;
>     end record;
> end Types_Test;
>
> Test program:
>
> with Types_Test; use Types_Test;
> procedure Test_Type
> is
>     M : Message_Event_Ptr;
>     Log : Events.Vector;
> begin
>     M := new Message_Event;
>     Message(M,"Test Message");
>     Log.Append(M);
> end Test_Type;
 >
> The idea is that Events.Vector should be able to hold different types of
> Events. Yet if I compile it this way, GNAT bombs out with:

    Log.Append (Message ("Test Message"));

> test_type.adb:9:15: expected type "Event_Ptr" defined at types_test.ads:5
> test_type.adb:9:15: found type "Message_Event_Ptr" defined at types_test.ads:7
>
> Obviously, I'm doing something wrong; I'm probably misunderstanding
> something, but what?

Ada has named types equivalence. Event_Ptr and Message_Event_Ptr are two 
independent types regardless what they point to. If you declared

    type Message_Event_Ptr is access Event'Class;

It would not work. Event if you did

    type Message_Event_Ptr is new Event_Ptr;

It still would not.

All this is unrelated to class-wide vs specific types.

BTW, it is a bad idea to use pointers, as bad as using vectors for a 
FIFO or Unbounded_String to keep constant strings. To dynamically 
allocate Unbounded_String which contents is again dynamically allocated 
is quite bizarre. Pointers could be used with queues when messages are 
pre-allocated elsewhere, in order to avoid copying overhead.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2016-03-20 12:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-20 11:15 Confused about class-wide types Mart van de Wege
2016-03-20 12:29 ` Dmitry A. Kazakov [this message]
2016-03-20 13:58   ` Mart van de Wege
2016-03-20 14:26     ` Dmitry A. Kazakov
2016-03-20 12:46 ` Simon Wright
2016-03-20 14:01   ` Mart van de Wege
2016-03-20 13:18 ` Shark8
2016-03-20 13:56   ` Mart van de Wege
2016-03-21 21:54   ` Randy Brukardt
replies disabled

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