From: tmoran@bix.com
Subject: Re: Inheritance and Polymorphism in Ada !!
Date: 1999/10/15
Date: 1999-10-15T00:00:00+00:00 [thread overview]
Message-ID: <McKN3.97$nP6.6657@typhoon-sf.snfc21.pbi.net> (raw)
In-Reply-To: 3806DC34.1513E8B1@frqnet.de
> 1 procedure inheritance_polymorphism is
> 2
> 3 type type_a is tagged record
> 4 first_field : integer;
> 5 end record;
> 6
> 7 type type_b is new type_a with record
> 8 second_field : integer;
> 9 end record;
> 10
> 11 type pointer_type is access all type_a'class;
> 12 pointer : pointer_type;
> 13
> 14 begin
> 15 pointer := new type_b;
> 16a type_b(pointer.all).second_field := 1;
> 16b pointer.second_field := 1;
> 17 end inheritance_polymorphism;
> See line 16, the type of the referenced object must be known in advance.
> With "pointer.second_field :=1" the compiler fails. It seems that the
> strong typing restricts the features of polyormism in Ada. Any comments?
Suppose you insert, just after line 15,
if phase_of_moon = full then pointer := new type_a;end if;
Then at run time the code would have to check that whatever "pointer"
was currently pointing to, contained a field named "second_field".
Further, since perhaps type_b's second_field might not be integer, eg
type type_b is new type_a with record
second_field : character;
end record;
it would have to check that the particular "second_field" was an
integer before it could assign a "1" to it.
Suppose "pointer.second_field := 1;" is in a subroutine called from
someplace where "pointer" points to some type_d, which might or might
not have a field named "second_field", which might or might not be an
integer. This isn't known at the time of compiling
"pointer.second_field := 1;" so the generated code would need to call
some routine associated with the tag on pointer.all and say "I'd like
to store an integer with value 1 in a field named "second_field'.
Please either do that or raise some appropriate exception."
Would you really want all that?
> See line 16, the type of the referenced object must be known in advance.
No. If you change line 11 to
> 11 type pointer_type is access all type_b'class;
then "pointer.second_field :=1;" compiles just fine, since it's
clear that anything the pointer might point to, does indeed have
an integer "second_field".
> With "pointer.second_field :=1" the compiler fails.
No, the compiler succeeds in detecting an error - before run time.
next prev parent reply other threads:[~1999-10-15 0:00 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-10-15 0:00 Inheritance and Polymorphism in Ada !! Chango Cho
1999-10-15 0:00 ` Lutz Donnerhacke
1999-10-15 0:00 ` Ted Dennison
1999-10-15 0:00 ` Andreas Winckler
1999-10-15 0:00 ` Lutz Donnerhacke
1999-10-15 0:00 ` Stephane Barbey
1999-10-15 0:00 ` Richard D Riehle
1999-10-15 0:00 ` Matthew Heaney
1999-10-15 0:00 ` Richard D Riehle
1999-10-18 0:00 ` Robert Dewar
1999-10-23 0:00 ` Richard D Riehle
1999-10-24 0:00 ` Robert Dewar
1999-10-24 0:00 ` Brian Rogoff
1999-10-26 0:00 ` Robert Dewar
1999-10-25 0:00 ` Robert A Duff
1999-10-26 0:00 ` Robert Dewar
1999-10-26 0:00 ` Robert A Duff
1999-10-18 0:00 ` Robert A Duff
1999-10-19 0:00 ` Robert Dewar
1999-10-20 0:00 ` Robert A Duff
1999-10-21 0:00 ` Paul Duquennoy
1999-10-21 0:00 ` Simon Wright
1999-10-21 0:00 ` Robert A Duff
1999-10-21 0:00 ` Robert Dewar
1999-10-22 0:00 ` Matthew Heaney
1999-10-15 0:00 ` tmoran [this message]
1999-10-15 0:00 ` tmoran
1999-10-15 0:00 ` Matthew Heaney
1999-10-18 0:00 ` Robert A Duff
1999-10-18 0:00 ` Brian Rogoff
1999-10-18 0:00 ` Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox