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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Confused about class-wide types Date: Sun, 20 Mar 2016 12:46:10 +0000 Organization: A noiseless patient Spider Message-ID: References: <86oaa97635.fsf@gaheris.avalon.lan> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="e717bf7a7fc57fe4d4f4e795918fa0ca"; logging-data="12835"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gALxVtPd+OgRknCIMeq4kwOGSnI58LC8=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (darwin) Cancel-Lock: sha1:jz9fq8Uo1yhvaPaFNu1aubfjQY4= sha1:3g11HN5iSvasdoJWyEWT6mRFhNY= Xref: news.eternal-september.org comp.lang.ada:29829 Date: 2016-03-20T12:46:10+00:00 List-Id: Mart van de Wege 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;