* Ada 95 dispatching question
@ 1997-11-26 0:00 Adam Beneschan
1997-11-26 0:00 ` Matthew Heaney
1997-11-27 0:00 ` Olivier Hainque
0 siblings, 2 replies; 4+ messages in thread
From: Adam Beneschan @ 1997-11-26 0:00 UTC (permalink / raw)
What should the following program output? I'm trying to get it to
output
3/TRUE/"abcdefghij"/ 15
but it outputs instead
3/TRUE/???
when compiled with GNAT 3.01.
If the output is correct, then why? In particular, is the line marked
"<=== HERE" a dispatching call, and if not, why not? And how would I
have to modify the program to get it to do what I'd like it to do,
given that declaring do_to_aux and do_to_rec in PACK1 is not an
option?
(My apologies if the problem is that I made a stupid mistake.)
-- thanks, Adam
package pack1 is
type auxinfotype is tagged null record;
type auxinfotype_p is access auxinfotype'class;
type rectype is record
f1 : integer;
f2 : boolean;
f3 : auxinfotype_p;
end record;
end pack1;
with pack1;
package pack2 is
type auxinfotype2 is new pack1.auxinfotype with null record;
procedure do_to_aux (a : in auxinfotype2);
procedure do_to_rec (r : in pack1.rectype);
end pack2;
with text_io;
package body pack2 is
procedure do_to_aux (a : in auxinfotype2) is
begin
text_io.put ("???");
end do_to_aux;
procedure do_to_rec (r : in pack1.rectype) is
begin
text_io.put (integer'image (r.f1) & "/" & boolean'image (r.f2) &
"/");
do_to_aux (auxinfotype2 (r.f3.all)); -- <=== HERE
text_io.new_line;
end do_to_rec;
end pack2;
with pack2;
package pack3 is
type auxinfotype3 is new pack2.auxinfotype2 with record
af1 : string (1 .. 10);
af2 : integer;
end record;
procedure do_to_aux (a : in auxinfotype3);
end pack3;
with text_io;
package body pack3 is
procedure do_to_aux (a : in auxinfotype3) is
begin
text_io.put ('"' & a.af1 & """/" & integer'image (a.af2));
end do_to_aux;
end pack3;
with pack1;
with pack2;
with pack3;
procedure test is
a : pack3.auxinfotype3;
r : pack1.rectype;
begin
a.af1 := "abcdefghij";
a.af2 := 15;
r.f1 := 3;
r.f2 := true;
r.f3 := new pack3.auxinfotype3' (a);
pack2.do_to_rec (r);
end test;
-------------------==== Posted via Deja News ====-----------------------
http://www.dejanews.com/ Search, Read, Post to Usenet
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Ada 95 dispatching question
1997-11-26 0:00 Ada 95 dispatching question Adam Beneschan
@ 1997-11-26 0:00 ` Matthew Heaney
1997-11-27 0:00 ` Olivier Hainque
1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1997-11-26 0:00 UTC (permalink / raw)
In article <880581829.29833@dejanews.com>, adam@irvine.com (Adam Beneschan)
wrote:
>with text_io;
>package body pack2 is
>
> procedure do_to_aux (a : in auxinfotype2) is
> begin
> text_io.put ("???");
> end do_to_aux;
>
> procedure do_to_rec (r : in pack1.rectype) is
> begin
> text_io.put (integer'image (r.f1) & "/" & boolean'image (r.f2) &
> "/");
> do_to_aux (auxinfotype2 (r.f3.all)); -- <=== HERE
> text_io.new_line;
> end do_to_rec;
>
>end pack2;
No, the marked line is NOT a dispatching call. You downcasted the root
type to a specific type - not a classwide type - and so do_to_aux binds
statically, to the version for auxinfotype2.
The rule in Ada is, you only dispatch on an object whose type is T'Class.
Had you downcasted to the classwide type auxinfotype2'Class, ie
do_to_aux (auxinfotype2'Class (r.f3.all));
then dispatching would occur.
--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Ada 95 dispatching question
1997-11-26 0:00 Ada 95 dispatching question Adam Beneschan
1997-11-26 0:00 ` Matthew Heaney
@ 1997-11-27 0:00 ` Olivier Hainque
1997-11-27 0:00 ` Olivier Hainque
1 sibling, 1 reply; 4+ messages in thread
From: Olivier Hainque @ 1997-11-27 0:00 UTC (permalink / raw)
Adam Beneschan (adam@irvine.com) wrote:
: What should the following program output? I'm trying to get it to
: output
: 3/TRUE/"abcdefghij"/ 15
: but it outputs instead
: 3/TRUE/???
The output is correct, ...
: If the output is correct, then why? In particular, is the line marked
: "<=== HERE" a dispatching call, and if not, why not? And how would I
: have to modify the program to get it to do what I'd like it to do,
: given that declaring do_to_aux and do_to_rec in PACK1 is not an
: option?
The call
: do_to_aux (auxinfotype2 (r.f3.all)); -- <=== HERE
is not a dispatching call since the actual argument you give is
not classwide typed.
In fact, you actually tell : "I want to call the do_to_aux procedure
with an argument of type 'auxinfotype2'".
For this call to be dispatching, you have to provide a classwide
argument. I think the following will do :
: do_to_aux (auxinfotype2'Class (r.f3.all));
Hope this helps.
-- Olivier Hainque
--
E-mail: hainque@inf.enst.fr
P-mail: Telecom Paris
46, rue Barrault - 75634 Paris cedex 13
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1997-11-27 0:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-11-26 0:00 Ada 95 dispatching question Adam Beneschan
1997-11-26 0:00 ` Matthew Heaney
1997-11-27 0:00 ` Olivier Hainque
1997-11-27 0:00 ` Olivier Hainque
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox