From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Actual stream of a certain type
Date: Thu, 27 Jul 2006 10:05:44 +0200
Date: 2006-07-27T10:05:44+02:00 [thread overview]
Message-ID: <kqdop3ewo06.z30ns7l2w4h5.dlg@40tude.net> (raw)
In-Reply-To: mailman.2.1153957636.32700.comp.lang.ada@ada-france.org
On Thu, 27 Jul 2006 00:47:00 +0100, Marius Amado-Alves wrote:
> Thanks, I know, I tried that, but I don't seem to succeed combining
> that with providing T_Stream as a mix-in. I also tried Ada 2005
> interfaces with GNAT. I thought I saw a real case for interfaces and
> that interested me because all examples I have seen so far are
> academic, not compelling. But failed. Every time Ada got in the way.
> It's really a love-hate relation with her.
You have a "parallel types hierarchy" problem, which in general has no
solution in Ada.
On Thu, 27 Jul 2006 01:25:04 +0100, in comp.lang.ada you wrote:
> Ok, here it goes. Of all idioms I tried maybe the least unsuccessful
> (?) ones are those that raise Program_Error with message
> "accessibility check failed" (GNAT) for line marked HERE below. The
> problem here is: how to use the stream visible on that line, as the
> good T_Stream object he is, pass it as a variable, etc? Thanks a
> great lot.
>
> type T_Stream_Class_Access is access all T_Stream'Class;
>
> procedure Write_T
> (Stream : T_Stream_Class_Access;
> Item : in T) is
> begin
> Foobar (Stream.all, Item);
> end;
>
> procedure Write
> (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
> Item : in T) is
> begin
> if Stream.all not in T_Stream'Class then
> raise Actual_Stream_Must_Be_A_T_Stream;
> else
> Write_T (T_Stream_Class_Access (Stream), Item); -- <== HERE
> end if;
> end;
I would use (double-dispatch emulation):
procedure Write_T (Stream : in out T_Stream; Item : T'Class) is
begin
Foobar (Stream, Item);
end Write_T;
procedure Write
( Stream : access Ada.Streams.Root_Stream_Type'Class;
Item : T
) is
begin
if Stream.all not in T_Stream'Class then
raise Actual_Stream_Must_Be_A_T_Stream;
else
Write_T (T_Stream'Class (Stream.all), Item);
end if;
end Write;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2006-07-27 8:05 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-26 17:27 Actual stream of a certain type Marius Amado-Alves
2006-07-26 18:25 ` tmoran
2006-07-26 23:47 ` Marius Amado-Alves
2006-07-27 8:05 ` Dmitry A. Kazakov [this message]
2006-07-27 8:45 ` Marius Amado-Alves
[not found] ` <2AA0FC04-5130-42BF-A6C9-03C3D6F4C642@amado-alves.info>
2006-07-27 10:47 ` Marius Amado-Alves
[not found] ` <71E9A504-B016-49E2-AB85-34CCDF0AEEC8@amado-alves.info>
2006-07-27 0:25 ` Marius Amado-Alves
[not found] <03AF72A2-6759-4DE5-9D52-4E8266C19C43@amado-alves.info>
2006-07-26 18:04 ` Marius Amado-Alves
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox