comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Assigning a "subclass" instance to a "superclass" variable
Date: Sat, 07 Apr 2012 12:18:32 +0200
Date: 2012-04-07T12:18:29+02:00	[thread overview]
Message-ID: <4f801475$0$6570$9b4e6d93@newsspool3.arcor-online.net> (raw)
In-Reply-To: <jlnkt5$91i$1@dont-email.me>

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.




      parent reply	other threads:[~2012-04-07 10:18 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
2012-04-07 10:18 ` Georg Bauhaus [this message]
replies disabled

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