comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Assigning a "subclass" instance to a "superclass" variable
Date: Sat, 07 Apr 2012 10:08:42 +0300
Date: 2012-04-07T10:08:42+03:00	[thread overview]
Message-ID: <9ua7fkFi6mU1@mid.individual.net> (raw)
In-Reply-To: <jlntb4$pm8$1@dont-email.me>

On 12-04-07 02:13 , deuteros wrote:
> On Fri 06 Apr 2012 05:50:18p, Simon Wright<simon@pushface.org>  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.<component> for any <component> 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
       .      @       .



  parent reply	other threads:[~2012-04-07  7:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-06 20:49 Assigning a "subclass" instance to a "superclass" variable deuteros
2012-04-06 21:50 ` Simon Wright
2012-04-06 23:13   ` deuteros
2012-04-07  7:06     ` Simon Wright
2012-04-07  7:06     ` Dmitry A. Kazakov
2012-04-07  7:08     ` Niklas Holsti [this message]
2012-04-07 10:18 ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox