comp.lang.ada
 help / color / mirror / Atom feed
* Assigning a "subclass" instance to a "superclass" variable
@ 2012-04-06 20:49 deuteros
  2012-04-06 21:50 ` Simon Wright
  2012-04-07 10:18 ` Georg Bauhaus
  0 siblings, 2 replies; 7+ messages in thread
From: deuteros @ 2012-04-06 20:49 UTC (permalink / raw)


I'm still trying to understand how Ada does object orientation. In Java I could
do this: 

Class Animal
{
  String color;

  public Animal(String color)
  {
    	this.color = color;
  }
}

Class Dog extends Animal
{
  public Dog(String color)
  {
    	super(color);
  }
}

So in Ada I'm trying to achieve something similar with my Statement
package: 

package Statements is

   type Statement is abstract tagged
      record
         tokens : Vector;
         executedtokens : Vector;
      end record;
   
   type Statement_Access is access all Statement'Class;
   
   function execute(skip: in Boolean; S: in Statement) return Integer is abstract;

end Statements;

So when I create a child of Statement, how to I set up the .ads file?



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Assigning a "subclass" instance to a "superclass" variable
  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 10:18 ` Georg Bauhaus
  1 sibling, 1 reply; 7+ messages in thread
From: Simon Wright @ 2012-04-06 21:50 UTC (permalink / raw)


deuteros <deuteros@xrs.net> writes:

> I'm still trying to understand how Ada does object orientation. In
> Java I could do this:
[...]
> So in Ada I'm trying to achieve something similar with my Statement
> package: 
>
> package Statements is
>
>    type Statement is abstract tagged
>       record
>          tokens : Vector;
>          executedtokens : Vector;
>       end record;
>    
>    type Statement_Access is access all Statement'Class;
>    
>    function execute(skip: in Boolean; S: in Statement) return Integer is abstract;
>
> end Statements;
>
> So when I create a child of Statement, how to I set up the .ads file?

   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;

or

   package Statements.Expressions is
      type Expression is new Statement with record
         ...
      end record;
      function Execute (Skip : Boolean; S : Expression) return Integer;
   end Statements.Expressions;

The second form is good if the full declaration of Statement is in the
private part of Statements, since child packages can see the private
parts of their parents.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Assigning a "subclass" instance to a "superclass" variable
  2012-04-06 21:50 ` Simon Wright
@ 2012-04-06 23:13   ` deuteros
  2012-04-07  7:06     ` Simon Wright
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: deuteros @ 2012-04-06 23:13 UTC (permalink / raw)


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;
> 
> or
> 
>    package Statements.Expressions is
>       type Expression is new Statement with record
>          ...
>       end record;
>       function Execute (Skip : Boolean; S : Expression) return Integer;
>    end Statements.Expressions;
> 
> The second form is good if the full declaration of Statement is in the
> private part of Statements, since child packages can see the private
> parts of their parents.

Okay, so would I need to put tokens and executedtokens in Expression's
record as well or would it somehow inherit them from Statements? 

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;

Now if the Expression package must have its own tokens and executedtokens
I assume this would be the way to do it but if not then how would this
work? 



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Assigning a "subclass" instance to a "superclass" variable
  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
  2 siblings, 0 replies; 7+ messages in thread
From: Simon Wright @ 2012-04-07  7:06 UTC (permalink / raw)


deuteros <deuteros@xrs.net> writes:

> 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;
>> 
>> or
>> 
>>    package Statements.Expressions is
>>       type Expression is new Statement with record
>>          ...
>>       end record;
>>       function Execute (Skip : Boolean; S : Expression) return Integer;
>>    end Statements.Expressions;
>> 
>> The second form is good if the full declaration of Statement is in
>> the private part of Statements, since child packages can see the
>> private parts of their parents.
>
> 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. See the Ada 95 Rationale:
http://www.adaic.org/resources/add_content/standards/95rat/rat95html/rat95-p1-2.html#1



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Assigning a "subclass" instance to a "superclass" variable
  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
  2 siblings, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2012-04-07  7:06 UTC (permalink / raw)


On Fri, 6 Apr 2012 23:13:40 +0000 (UTC), deuteros wrote:

> 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;
> 
> Now if the Expression package must have its own tokens and executedtokens
> I assume this would be the way to do it but if not then how would this
> work?

Then the parent type (Statement) should not have them in its
implementation. If children have token types of their own (a parallel types
hierarchy), you should provide an abstract interface to access
child-specific tokens. E.g.

   function Get_Token (S : Statement; Index : Token_Index_Type)
      return Token_Type'Class is abstract;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Assigning a "subclass" instance to a "superclass" variable
  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
  2 siblings, 0 replies; 7+ messages in thread
From: Niklas Holsti @ 2012-04-07  7:08 UTC (permalink / raw)


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
       .      @       .



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Assigning a "subclass" instance to a "superclass" variable
  2012-04-06 20:49 Assigning a "subclass" instance to a "superclass" variable deuteros
  2012-04-06 21:50 ` Simon Wright
@ 2012-04-07 10:18 ` Georg Bauhaus
  1 sibling, 0 replies; 7+ messages in thread
From: Georg Bauhaus @ 2012-04-07 10:18 UTC (permalink / raw)


On 06.04.12 22:49, deuteros wrote:
> I'm still trying to understand how Ada does object orientation.

Just for completeness,

type Animal is...
    .
   / \
  ´ ~ `
    |
type Dog is new Animal...

Each type declaration is typically followed by a number
of declarations of subprograms that take, or return, at least
one argument of the preceding type. These subprograms become
the primitive operations of the preceding type (its methods),
overriding operations where the parent type has the "same"
operation.

Placing the type declarations in packages at all, in nested
packages, in task bodies, in child packages, declaring
them local to a procedure, etc ... is the programmer's choice:
doing any of these will establish corresponding visibility
of the types. So the key question might be where you want
the types visible.

> So in Ada I'm trying to achieve something similar with my Statement
> package:
>
> package Statements is
>
>     type Statement is abstract tagged
>        record
>           tokens : Vector;
>           executedtokens : Vector;
>        end record;
>
>     type Statement_Access is access all Statement'Class;

Do you need this pointer type right here or in any child packages
of Statements, and in such a way that it is globally visible,
and in scope everywhere Statement is? If so, do the pointers need
to be pointing to non-constant Statement objects on the stack
as well? If not, you could start by omitting the "all", or replace
"all" by "constant".

Insofar as tagged objects are by-reference types already,
explicit pointers are rarely needed for "O-O effects". This is somewhat
like Java, which, for O-O objects, passes references. (But needs
"new" allocators when creating O-O objects, unlike Ada.)
Pointers can become necessary in case one record type needs
to have a component of any type from a hierarchy like Animal'Class.
That's because the specific type isn't known yet: could be Dog, Pig,
Hound_Dog, Spider, ..., but the compiler needs to establish the
'Size of the record type, hence indirection because a pointer
has a known size. It's like declaring a field of type Animal
in a Java class and assigning it a Dog object reference,
or Pig object reference, etc.


> So when I create a child of Statement, how to I set up the .ads file?

As said, you do not even need to put a child type in its own package.
If you choose to pick from the setups Simon has shown,
and if such a package should be translated with GCC, the default
method requires file names to be "expressions.ads" and
"statements-expressions.ads", respectively.




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-04-07 10:18 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2012-04-07 10:18 ` Georg Bauhaus

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