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!feeder.eternal-september.org!news.freedyn.net!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Confused about class-wide types Date: Sun, 20 Mar 2016 13:29:45 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <86oaa97635.fsf@gaheris.avalon.lan> NNTP-Posting-Host: LMk7+sG0YqgPmReI4fVkAA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:29828 Date: 2016-03-20T13:29:45+01:00 List-Id: 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