comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Confused about class-wide types
Date: Sun, 20 Mar 2016 12:46:10 +0000
Date: 2016-03-20T12:46:10+00:00	[thread overview]
Message-ID: <ly4mc1xqp9.fsf@pushface.org> (raw)
In-Reply-To: 86oaa97635.fsf@gaheris.avalon.lan

Mart van de Wege <mvdwege@gmail.com> writes:

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

You don't need this; Event_Ptr covers Message_Event'Class.

>    procedure Message (M : in out Message_Event_Ptr;
> 		      Mess : in String);

M should be of type Message_Event.

> Test program:
>
> with Types_Test; use Types_Test;
> procedure Test_Type
> is
>    M : Message_Event_Ptr;

This should be of type Event_Ptr.

>    Log : Events.Vector;
> begin
>    M := new Message_Event;
>    Message(M,"Test Message");

Now it gets tricky.

   Message(Message_Event(M.all),"Test Message");

works, because M.All is of type Event'Class and we can do a view
conversion to Message_Event (that's what it was created as in the first
place; we'd get CE if it wasn't).

I think this is difficult because you've introduced operations for the
child (Message_Event) of which the parent (Event) has no analogue at
all. What is the common operation that all Events must support?
Maybe something like

   procedure Handle (E : Event) is abstract;

Your procedure Message is a factory, I think; it might be better as

   function Message (Mess : in String) return Message_Event;

  parent reply	other threads:[~2016-03-20 12:46 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
2016-03-20 13:58   ` Mart van de Wege
2016-03-20 14:26     ` Dmitry A. Kazakov
2016-03-20 12:46 ` Simon Wright [this message]
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