comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Barnes vs. Dewar
Date: 1999/08/27
Date: 1999-08-27T00:00:00+00:00	[thread overview]
Message-ID: <37c6a288@news1.us.ibm.net> (raw)
In-Reply-To: 37C66E08.16DB@dera.gov.uk

In article <37C66E08.16DB@dera.gov.uk> , Anton Gibbs <agibbs@dera.gov.uk> 
wrote:

> package Tracked_Things is
>
>    type Identity_Controlled is abstract tagged private;
>
> private
>
>    type Identity_Controlled is abstract new Controlled with
>    record
>       Identity_Number : Integer;
>    end record;
>
>    procedure Initialize( X : in Identity_Controlled );
>    -- etc.
>
> end Tracked_Things;
>
> package Tracked_Things.User_View is
>
>    type Thing is new Identity_Controlled with
>    record
>       UU : Integer;
>    end record;
>
> end Tracked_Things.User_View;
>
> [There are few with/use clauses implied here].
>
> Page 287, third paragraph from the end, last sentance reads:
>
>    "We also declare Initialize, Adjust and Finalize in the private part
> and so they are also hidden from the user."

That is the proper thing to do.  Controlled-ness for a type should be
completely hidden from clients.

> Clearly, these operations are expected to be inherited for the extended
> type `Thing' in child package `Tracked_Things.User_View'.

Yes, they are indeed inherited, but only for a private view.  A private view
of Thing is

o the body of Tracked_Thing.User_View

o anywhere in a private child of Tracked_Thing.User_View

o the private part of the spec of a public child of Tracked_Thing.User_View,
and its body


> Well, when I tried a similar thing with the GNAT compiler it threw it
> out complaining that:
>
>    "Initialize" is not a visible entity of "User_View"


But, are you sure you didn't try to do this:

  with Tracked_Thing.User_View;  use Tracked_Thing.User_View;
  procedure Op is
    O : Thing;
  begin
    Initialize (O);
  end Op;

This is of course illegal, because Op isn't a private view of Thing.

Try this instead:

  private procedure Tracked_Thing.User_View.Op;

  procedure Tracked_Thing.User_View.Op is
    O : Thing;
  begin
    Initialize (O);
  end;

This should work, but I don't have a compiler handy to test it.

> After the usual agony with the LRM, I eventually had to agree. I do not
> think that the child package `Tracked_Things.User_View' is entitled to
> see `Initialize' in the private part of its parent and so it cannot be
> inherited. If you move `Initialize' to the visible part it all works
> fine.

Not quite right.  Only the public part of Tracked_Things.User_View is not
allowed to see Initialize.  The operations Init, Final, and Adj are indeed
inherited, and you can see them in the private part of the spec of
Tracked_Things.User_View, and in its body.

Try this:

package Tracked_Things.User_View is

  type Thing is new Identity_Controlled with
  record
    UU : Integer;
  end record;

private

  procedure Initialize (O : in out Thing);  -- override; should compile OK

end Tracked_Things.User_View;

or try this:

<original spec of Tracked_Things.User_View>

package body Tracked_Things.User_View is
  O : Thing;
begin
  Initialize (O);  -- visible, because body is a private view
end;

> So who is right Barnes or Dewar ?

Barnes is right.  If you tried to call Initialize from a public view, then
Dewar is also correct.

> More to the point, what is the correct way to achieve the desired level
> of visibility (ie. `Tracked_Things.User_View.Initialize' visible
> but `Tracked_Things.Initialize' not) ?

This doesn't make any sense.  The operation Initialize is private for ALL
types in the class whose root is Identity_Controlled, including Thing.  You
can NEVER call Initialize (Object_Of_Type_Thing) from a public view.

> Thanks for any clues.

If you want to have a public operation for Thing that does some kind of
initialization (maybe even implement it by calling the controlled operation
called Initialize), then you're going to have to give the operation some
other name.

package Tracked_Things.User_View is

  type Thing is new Identity_Controlled with
  record
    UU : Integer;
  end record;

  procedure Reset (O : in out Thing);

end Tracked_Things.User_View;


package body Tracked_Things.User_View is

  procedure Reset (O : in out Thing) is
  begin
    Initialize (O);  -- should be legal
  end;

end Tracked_Things.User_View;


--
Matt

It is impossible to feel great confidence in a negative theory which has
always rested its main support on the weak points of its opponent.

Joseph Needham, "A Mechanistic Criticism of Vitalism"




  reply	other threads:[~1999-08-27  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-08-27  0:00 Barnes vs. Dewar Anton Gibbs
1999-08-27  0:00 ` Matthew Heaney [this message]
1999-08-27  0:00 ` Robert A Duff
1999-08-27  0:00   ` Matthew Heaney
1999-08-30  0:00     ` Robert A Duff
1999-08-29  0:00 ` Robert Dewar
replies disabled

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