comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: OOP in Ada: Alternatives for "protected" scope
Date: Thu, 3 May 2012 23:56:51 +0200
Date: 2012-05-03T23:56:51+02:00	[thread overview]
Message-ID: <1vn9nv4aqm9a3.1tqzc91idjlbs$.dlg@40tude.net> (raw)
In-Reply-To: jnut8r$aqb$1@online.de

On Thu, 3 May 2012 23:27:55 +0200, Felix Krause wrote:

> I have some abstract class A, defined in package P_A. This class 
> provides some functionality as procedure Do_Something. Now there is 
> some calculation to be done while execution Do_Something which cannot 
> be defined in A. Therefore, it is left to the specific child classes of 
> abstract class A to implement that calculation. The code might look 
> like this:

In that case Do_Something cannot be a primitive operation or else the
operation called from it must re-dispatch.

> package P_A is
>    type A is abstract tagged private;
> 
>    procedure Do_Something (Object : in out A);
>
>    function Calculate (Object : in out A) return Integer is abstract;

This is illegal in Ada 95-2005.

> private
>    -- define A here...
> end P_A;
> 
> package body P_A is
>    procedure Do_Something (Object : in out A) is
>       Var : Integer;
>    begin
>       -- some useful code here嚙皺
> 
>       -- let the specific child class define this calculation
>       Var := Calculate (Object);

This is wrong, it should have been

   Var := Calculate (A'Class (Object));

or else Do_Something should be declared class-wide:

   procedure Do_Something (Object : in out A'Class);

>       -- some more useful code here...
>    end Do_Something;
> end P_A;
>
> The function Calculate should only be used internally. I do not see a 
> good way to enforce this with Ada: I do not want to move it to P_A's 
> private part, because child classes of A should not be required to be 
> located in sub-packages of P_A. (It's also forbidden for an abstract 
> function to reside in the private part.)

If Do_Something varies in children they should override it. The reusable
parts common for all children can be factored out as a private class-wide
operation.

All children having access to private operations must be declared in
children packages.

   package P is
      type A is abstract tagged private;   
      procedure Do_Something (Object : in out A) is abstract;
   private
      ...
      procedure Do_Prologue  (Object : in out A'Class);
      procedure Do_Epilogue  (Object : in out A'Class);
   end P;

   package P.Q is
      type B is new A with private;
      overriding procedure Do_Something (Object : in out B);
      ...
   end P.Q;

   package body P.Q is
      procedure Do_Something (Object : in out B) is
      begin
          Do_Prologue (Object);
          ... -- Do specific calculations
          Do_Epilogue (Object);
      end Do_Something;
   end P.Q;

> In languages like Java and C#, there is a "protected" scope to cope 
> with this situation.

This is exactly what Ada's private is. The correspondence goes as follows:

"Public" <-> Ada' public operation
"Protected" <-> Ada's private operation
"Private" <-> An operation defined in the package body

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



  reply	other threads:[~2012-05-03 21:57 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-03 21:27 OOP in Ada: Alternatives for "protected" scope Felix Krause
2012-05-03 21:56 ` Dmitry A. Kazakov [this message]
2012-05-04 18:48   ` Felix Krause
2012-05-04 22:00     ` Dmitry A. Kazakov
2012-05-05  8:28       ` Felix Krause
2012-05-05 11:53         ` Dmitry A. Kazakov
2012-05-05  4:33     ` Shark8
2012-05-03 23:34 ` Randy Brukardt
2012-05-04  1:41 ` BrianG
2012-05-04 10:37 ` Georg Bauhaus
2012-05-04 10:43   ` Georg Bauhaus
2012-05-04 12:10   ` Dmitry A. Kazakov
replies disabled

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