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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7508aa0d80b8bf66 X-Google-Attributes: gid103376,public From: tmoran@bix.com Subject: Re: Inheritance and Polymorphism in Ada !! Date: 1999/10/15 Message-ID: #1/1 X-Deja-AN: 537173942 References: <3806DC34.1513E8B1@frqnet.de> X-Complaints-To: abuse@pacbell.net X-Trace: typhoon-sf.snfc21.pbi.net 940011436 207.214.211.239 (Fri, 15 Oct 1999 11:17:16 PDT) Organization: SBC Internet Services NNTP-Posting-Date: Fri, 15 Oct 1999 11:17:16 PDT Newsgroups: comp.lang.ada Date: 1999-10-15T00:00:00+00:00 List-Id: > 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.