comp.lang.ada
 help / color / mirror / Atom feed
* Problems with tagged records and inheritance
@ 2003-07-10 20:56 Papastefanos Serafeim
  2003-07-10 22:15 ` Re; " tmoran
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Papastefanos Serafeim @ 2003-07-10 20:56 UTC (permalink / raw)


I have using a base type like this:
...
type Base is tagged private;
...
type Base is tagged record
         AAA: Integer:=1;
      end record;

and a child type like this

type Child is new Base with private;
....
type Child is new Base with record
        BBB: Integer:=5;
      end record;

The problem is that the following is not working:

   procedure Test(Ch: in Child) is
   begin
      Put(Ch.AAA); --<- This line has an error, it says no selector AAA for
type Child
      Put(Ch.BBB);
   end Test;
The procedure Test is declared in the same package
as the type Child and defined at the package's Body.

The error is becouse AAA is not part of Child.
Why is that ? I thought that Child would contain
AAA and BBB, and not only BBB...

--
Papastefanos Serafeim
serafeim@otenet.gr





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

* Re; Problems with tagged records and inheritance
  2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
@ 2003-07-10 22:15 ` tmoran
  2003-07-10 22:24 ` Robert I. Eachus
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: tmoran @ 2003-07-10 22:15 UTC (permalink / raw)


> The error is becouse AAA is not part of Child.
> Why is that ? I thought that Child would contain
> AAA and BBB, and not only BBB...
  But you said:
>type Base is tagged private;
so the makeup of Base is private, as you requested, and not visible to Child.



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

* Re: Problems with tagged records and inheritance
  2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
  2003-07-10 22:15 ` Re; " tmoran
@ 2003-07-10 22:24 ` Robert I. Eachus
  2003-07-10 22:29 ` Ludovic Brenta
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Robert I. Eachus @ 2003-07-10 22:24 UTC (permalink / raw)


Papastefanos Serafeim wrote:
> I have using a base type like this:
> ...
> type Base is tagged private;
> ...
-- Foot, bullet, shoot.
> type Base is tagged record
>          AAA: Integer:=1;
>       end record;
> 
> and a child type like this
> 
> type Child is new Base with private;
> ....
> type Child is new Base with record
>         BBB: Integer:=5;
>       end record;
> 
> The problem is that the following is not working:
> 
>    procedure Test(Ch: in Child) is
>    begin
>       Put(Ch.AAA); --<- This line has an error, it says no selector AAA for
> type Child
>       Put(Ch.BBB);
>    end Test;
> The procedure Test is declared in the same package
> as the type Child and defined at the package's Body.

Why would you expect this to work?  You explicitly limited the knowledge 
of AAA to within the package where Base is declared, and in its 
childred.  You can "fix" the problem by adding an inquiry function to 
the package containing Base:  Put(Contents(Base(Ch))); (You need a view 
conversion to call it.)  Or you can fix it by making the package that 
declares Child a child package of the one that contains Base.  Or you 
can just make AAA public.

I don't know what the "right" fix is because I have no idea what you are 
trying to do.  But information hiding in Ada is not partial.  There are 
some interesting issues that come up with three levels of derivation, 
and even with two levels you could have Child with two components named 
AAA, but never visible in the same contexts...

> The error is becouse AAA is not part of Child.
> Why is that ? I thought that Child would contain
> AAA and BBB, and not only BBB...

It contains both.  The issue is where each is visible.  As I pointed 
out, both could be named AAA (not a change you want to make).  Since 
either AAA or BBB is visible but not both, there would be no problem if 
they had the same name.


-- 

                                                        Robert I. Eachus

�In an ally, considerations of house, clan, planet, race are 
insignificant beside two prime questions, which are: 1. Can he shoot? 2. 
Will he aim at your enemy?� -- from the Laiden novels by Sharon Lee and 
Steve Miller.




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

* Re: Problems with tagged records and inheritance
  2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
  2003-07-10 22:15 ` Re; " tmoran
  2003-07-10 22:24 ` Robert I. Eachus
@ 2003-07-10 22:29 ` Ludovic Brenta
  2003-07-11 11:56   ` Marin David Condic
  2003-07-11 11:49 ` Papastefanos Serafeim
  2003-07-15  1:34 ` Richard Riehle
  4 siblings, 1 reply; 7+ messages in thread
From: Ludovic Brenta @ 2003-07-10 22:29 UTC (permalink / raw)


"Papastefanos Serafeim" <serafeim@otenet.gr> writes:

> The error is becouse AAA is not part of Child.
> Why is that ? I thought that Child would contain
> AAA and BBB, and not only BBB...

I don't have all the details of your program, but I suspect that:

- you have declared the two types Base and Child in different packages

- you have defined the full view of Base in Base's private part

- and that the package containing Child is not a child package of the
  one where you defined Base.

This would explain that your Test procedure can only see the part of
the Ch object that is declared in the same package.  The part
inherited from Base is private.

If you want the Test procedure to see the internal details of Base,
you must either declare Child in a child package or in the same
package as Base.

On the other hand, this would break encapsulation; is this really what
you want?  Here is a solution that does not break encapsulation:

package One is
   type Base is tagged private;
   procedure Test (B : in Base);
private
   type Base is tagged record  -- The full view is private
      Aaa : Integer;
   end record;
end One;

package body One is
   procedure Test (B : in Base) is
   begin
      Put (B.Aaa);
   end Test;
end One;

with One; use One;
package Two is
   --  Package Two is not a child of package One; therefore it cannot
   --  see the private part of package One.
   type Child is new Base with private;

   procedure Test (Ch : in Child);
private
   type Child is new Base with record
      Bbb : Integer;
   end record;
end Infant;


package body Two is
   procedure Test (Ch : in Child) is
   begin
      One.Test (Base (Ch)); -- Delegate processing of invisible part
      --  Now, process the visible part:
      Put (Ch.Bbb);
   end Test;
end Two;


HTH

-- 
Ludovic Brenta.



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

* Re: Problems with tagged records and inheritance
  2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
                   ` (2 preceding siblings ...)
  2003-07-10 22:29 ` Ludovic Brenta
@ 2003-07-11 11:49 ` Papastefanos Serafeim
  2003-07-15  1:34 ` Richard Riehle
  4 siblings, 0 replies; 7+ messages in thread
From: Papastefanos Serafeim @ 2003-07-11 11:49 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 944 bytes --]

I got it, thanks for the answers...

� Papastefanos Serafeim ������ ��� ������ ...
>I have using a base type like this:
>...
>type Base is tagged private;
>...
>type Base is tagged record
>         AAA: Integer:=1;
>      end record;
>
>and a child type like this
>
>type Child is new Base with private;
>....
>type Child is new Base with record
>        BBB: Integer:=5;
>      end record;
>
>The problem is that the following is not working:
>
>   procedure Test(Ch: in Child) is
>   begin
>      Put(Ch.AAA); --<- This line has an error, it says no selector AAA for
>type Child
>      Put(Ch.BBB);
>   end Test;
>The procedure Test is declared in the same package
>as the type Child and defined at the package's Body.
>
>The error is becouse AAA is not part of Child.
>Why is that ? I thought that Child would contain
>AAA and BBB, and not only BBB...
>
>--
>Papastefanos Serafeim
>serafeim@otenet.gr
>
>





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

* Re: Problems with tagged records and inheritance
  2003-07-10 22:29 ` Ludovic Brenta
@ 2003-07-11 11:56   ` Marin David Condic
  0 siblings, 0 replies; 7+ messages in thread
From: Marin David Condic @ 2003-07-11 11:56 UTC (permalink / raw)


That looks like a really helpful answer for an Ada newbie. It 
illustrates how to fix the problem very well. Just in case the OP had 
reasons to want visibility of AAA, we should point out that the way to 
create a child package looks something like this:

package Base_Class is
...
type Base is tagged private;
...
type Base is tagged record
          AAA: Integer:=1;
       end record;

end Base_Class ;

...

package Base_Class.Child_Class is

type Child is new Base with private;
....
type Child is new Base with record
         BBB: Integer:=5;
       end record;

end Base_Class.Child_Class ;

Now the child package would have full knowledge of what is inside the 
private part of the Base_Class package and could reference the AAA field.

Note that there are reasons you may not want to do that. Gaining 
visibility to the private part of the package pretty much violates the 
whole concept of why it was made private in the first place. Generally, 
providing accessor functions for the fields of interest or procedures 
for other operations on the hidden information is a better choice.

I generally like to go to a child package with full visibility of the 
private section in cases where I am conveniently sectioning off 
operations for a class. The parent may have basic accessor & 
construction/destruction operations. One child may provide operations 
for I/O to/from files, another might provide display capabilities, etc. 
Where I do type extension is when I am providing a family of classes to 
the user so they can pick the best one for the job. There is a little 
bit of an art to deciding when something should be a child package and 
when it should not, but you develop a sense for it with some experience 
building larger systems in Ada.

MDC





Ludovic Brenta wrote:
> 
> I don't have all the details of your program, but I suspect that:
> 


-- 
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

     "In general the art of government consists in taking as
     much money as possible from one class of citizens to give
     to the other."

         --  Voltaire
======================================================================




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

* Re: Problems with tagged records and inheritance
  2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
                   ` (3 preceding siblings ...)
  2003-07-11 11:49 ` Papastefanos Serafeim
@ 2003-07-15  1:34 ` Richard Riehle
  4 siblings, 0 replies; 7+ messages in thread
From: Richard Riehle @ 2003-07-15  1:34 UTC (permalink / raw)


I am including your posting at the end of this reply.

First, you have a visibility problem because type Base
is private and its contents are not visible.   Also, you have
a design problem.   Consider,

       package The_Base is
           type Base is tagged private;
           procedure Put(B : Base);
       private
          type Base is tagged record
                AAA :  Integer :=  42;
          end record;
       end Base;

Now you have a design with a primitive operation for Base.  You
would need more of these, of course.

      package Derived_From_Base is
           type Derived is new The_Base.Base with private;
           procedure Put(D : Derived);
       private
          type Derived is new The_Base.Base with record
                BBB :  Integer :=  451;
          end record;
      end Derived_From_Base;

Now you have a derived type  along with a primitive Put
method for it.   You implement the Put in terms of the existing
Put in The_Base.

     with Ada.Integer_Text_IO;
     package body Derived_From_Base is
      procedure Put(D : Derived) is
      begin
          The_Base.Put(The_Base.Base(D));
          Ada.Integer_Text_IO.Put(BBB);
      end Put;
    end Derived_From_Base;

One of the key ideas behind object-oriented programming
is that of reuse.   In the Put method for Derived you are
first reusing the implementation of the Put for its parent and
then coding the part for the immediately visible type.

Hope this helps.  I did not compile this so there might be some typos,
but the fundamental idea is there.

Richard Riehle

Papastefanos Serafeim wrote:

> I have using a base type like this:
> ...
> type Base is tagged private;
> ...
> type Base is tagged record
>          AAA: Integer:=1;
>       end record;
>
> and a child type like this
>
> type Child is new Base with private;
> ....
> type Child is new Base with record
>         BBB: Integer:=5;
>       end record;
>
> The problem is that the following is not working:
>
>    procedure Test(Ch: in Child) is
>    begin
>       Put(Ch.AAA); --<- This line has an error, it says no selector AAA for
> type Child
>       Put(Ch.BBB);
>    end Test;
> The procedure Test is declared in the same package
> as the type Child and defined at the package's Body.
>
> The error is becouse AAA is not part of Child.
> Why is that ? I thought that Child would contain
> AAA and BBB, and not only BBB...
>
> --
> Papastefanos Serafeim
> serafeim@otenet.gr







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

end of thread, other threads:[~2003-07-15  1:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
2003-07-10 22:15 ` Re; " tmoran
2003-07-10 22:24 ` Robert I. Eachus
2003-07-10 22:29 ` Ludovic Brenta
2003-07-11 11:56   ` Marin David Condic
2003-07-11 11:49 ` Papastefanos Serafeim
2003-07-15  1:34 ` Richard Riehle

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