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=0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,da9f9995bafb51df X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.74.201 with SMTP id w9mr829514pbv.0.1333782517859; Sat, 07 Apr 2012 00:08:37 -0700 (PDT) Path: r9ni28082pbh.0!nntp.google.com!news1.google.com!news3.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Assigning a "subclass" instance to a "superclass" variable Date: Sat, 07 Apr 2012 10:08:42 +0300 Organization: Tidorum Ltd Message-ID: <9ua7fkFi6mU1@mid.individual.net> References: Mime-Version: 1.0 X-Trace: individual.net im0lvIdIggf8Fhdyxl9kfg+Y+gUqq6+dHiwT3o0tWqzYJgkjsO Cancel-Lock: sha1:PX+a5d1fKRWSfSBg61u0qsr6Uo8= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2012-04-07T10:08:42+03:00 List-Id: On 12-04-07 02:13 , deuteros wrote: > On Fri 06 Apr 2012 05:50:18p, Simon Wright wrote in > news:m2ty0wh6n9.fsf@pushface.org: > >> with Statements; >> package Expressions is >> type Expression is new Statements.Statement with record >> ... >> end record; >> function Execute (Skip : Boolean; S : Expression) return Integer; >> end Expressions; .. snip .. > Okay, so would I need to put tokens and executedtokens in Expression's > record as well or would it somehow inherit them from Statements? They are inherited. In the form type Expression is new Statement with record ... end record; the part "new Statement" brings in the components of the Statement type, and the part "with record ... end record" adds new components for Expression. These components *extend* the Statement record. If you have a variable E : Expression, you can refer to E.tokens and E.executedtokens (assuming that this point in the code has visibility on the structure of a Statement). And of course you can refer to E. for any declared in the "with record...end record" (again assuming visibility). > I want something like a constructor in my child classes: > > function Create_Expression (tokens, executedtokens : Vector) return Expression is > E : Expression > begin > E.tokens := tokens; > E.executedtokens := executedtokens; > return E; > end Create_Expression; That should work. But note that Ada does not have a special sort of "constructor" functions, such as C++ and Java do, so the above is just an ordinary (primitive) function in Ada, with the same rules and syntax. (There have been several discussions of this topic on c.l.a in the past, and some think that the this is a flaw in Ada.) -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .