comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Separate Compilation in Programming Languages
Date: Sun, 24 Feb 2008 12:20:02 -0500
Date: 2008-02-24T12:20:02-05:00	[thread overview]
Message-ID: <wccve4eff0d.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: c39wj.4638$Mh2.4179@nlpi069.nbdc.sbc.com

<adaworks@sbcglobal.net> writes:

> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message 
> news:wccbq679zah.fsf@shell01.TheWorld.com...
>
> In the example you provided (shown below) you did defer the with clause
> for the Package P1 to the package body of P2.   This illustrates my point.
> If the specification for P1 changes, only the body for P2 needs to be  compiled.
> Had the with clause been at the specification level for P2, and had the
> specification for P1 changed, the entire package would have had to be
> compiled.

Earlier, you said that this conversation was not about compilation
costs, but now you seem quite interested in compilation costs.
That's OK.  So let's talk about compilation costs.  ;-)

I think you miss the point of my example (still shown below).
Package P1 is the interface, and P2 is the implementation
of that interface.  P2 is NOT a client of P1, it's an
implementation of P1!

There are perhaps 37 clients of P1, not shown.  The question is,
if we modify the implementation (i.e. P2) do we need to recompile
those 37 clients.  The answer is "no".

As you point out, if you change the interface, you have to recompile the
implementation.  That's true in my example.  It's also true if
the interface/implementation is split in the more traditional
Ada way (spec/body).

The issue of recompiling the spec of P2 is irrelevant, since it's empty!

> I think the same is true for Java.   If the specification for the Interface is
> changed, everything needs to be recompiled.   In Ada, by deferring the
> with clause to the package body, we only need to re-compile the body.
>
> So, I think your example actually supports my original assertion.  The
> essential point is that changes in the specifications for a package (or
> a Java class) should not require re-compilation of the dependent
> specifications.   Only the implementations should be dependent on
> other specifications.   This has the effect of keeping the high-level
> architecture more stable.
>
>   package P1 is
>         type Iface is interface;
>         ... declare some abstract procedures ...
>     end P1;
>
>     package P2 is
>         pragma Elaborate_Body;
>         -- Note that this package spec can be entirely empty (except we
>         -- need the pragma so we are allowed to have a body).
>         -- So this package spec isn't defining any interface to
>         -- anything at all!
>     end P2;
>
>     with P1;
>     package body P2 is
>         type Impl is new P1.Iface with ...
>         ... override procedures ...
>         ...
>     end P2;
>
>     -- No body for P1!
>
> Then the client can "with P1", and call operations on Iface objects
> without knowing anything about package P2 (neither spec nor body).
> There is no dependence of clients on P2 (unless I'm misunderstanding
> what you mean by "depend").

Let me try a more concrete example -- the trusty stack example.

First, the traditional Ada way, using what you like to call "opaque"
types:

    package Stacks is
        type Stack is private;
        procedure Push(...);
        ...
    private
        type Stack_Rec;
        type Stack is access Stack_Rec;
    end Stacks;

    package Stacks is
        type Stack_Rec is record ...;
        procedure Push(...) is
        begin
           ...
        end Push;
        ...
    end Stacks;

If we change the implementation of the stacks abstraction (i.e. the body
of package Stacks), we do not normally need to recompile the clients.

We can accomplish the same thing with interfaces.  I'll use a child
package this time, just for fun:

    package Stacks is
        type Stack is interface;
        procedure Push(...) is abstract;
        ...
    end Stacks;

    package Stacks.Implementation is
        type Stack_Impl is new Stack with private;
    private
        type Stack_Impl is ...;
        overriding procedure Push(...);
        ...
    end Stacks.Implementation;

    package Stacks.Implementation is

        procedure Push(...) is
        begin
           ...
        end Push;
        ...
    end Stacks.Implementation;

If we change the implementation of the stacks abstraction (i.e. the
package Stacks.Implementation -- spec or body, it doesn't matter), we do
not normally need to recompile the clients.

Both ways of writing the stacks abstraction have the same property --
the 37 clients that say "with Stacks" do not need to be recompiled when
we modify the implementation.

The second example matches what is normally done in Java, except that in
Java, we wouldn't split the implementation into two parts.  Note that in
the second example, that split has no value, since there are no clients
of Stacks.Implementation.  In fact, in some cases, we can choose to move
all of the code from the spec of Stacks.Implementation to its body (as I
did in my P1/P2 example), making the spec empty.

(I'm glossing over the fact that there probably needs to be some sort of
factory for creating stack objects, but that's not relevant to my
point.)

- Bob



  reply	other threads:[~2008-02-24 17:20 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-22 17:35 Separate Compilation in Programming Languages adaworks
2008-02-22 17:52 ` Dirk Heinrichs
2008-02-23  0:23   ` adaworks
2008-02-22 18:10 ` Ray Blaak
2008-02-22 23:53   ` adaworks
2008-02-23  1:40     ` Ray Blaak
2008-02-23  7:29       ` adaworks
2008-02-23 18:42         ` Ray Blaak
2008-02-22 18:18 ` Niklas Holsti
2008-02-23  0:14   ` adaworks
2008-02-23  7:23     ` Niklas Holsti
2008-02-23  7:31       ` Niklas Holsti
2008-02-23 16:00         ` adaworks
2008-02-23 12:27     ` Georg Bauhaus
2008-02-23 12:37   ` Dirk Heinrichs
2008-02-23 13:03     ` Niklas Holsti
2008-02-22 19:11 ` Dmitry A. Kazakov
2008-02-23  0:16   ` adaworks
2008-02-22 19:33 ` Larry Kilgallen
2008-02-22 20:47 ` Simon Wright
2008-02-22 21:27 ` Robert A Duff
2008-02-23  0:12   ` adaworks
2008-02-23 10:28     ` framefritti
2008-02-23 12:45     ` Dirk Heinrichs
2008-02-23 15:16     ` Robert A Duff
2008-02-23 16:47       ` adaworks
2008-02-23 18:47         ` Ray Blaak
2008-02-24  7:40           ` adaworks
2008-02-24  9:42             ` Ray Blaak
2008-02-24 20:41               ` adaworks
2008-02-25  2:37                 ` Ray Blaak
2008-02-25  7:06                   ` adaworks
2008-02-25 13:12                     ` Robert A Duff
2008-02-25 17:44                     ` Ray Blaak
2008-02-25 22:16                       ` Ray Blaak
2008-02-26  5:10                         ` John W. Kennedy
2008-02-26 19:08                           ` Ray Blaak
2008-02-26  7:11                         ` adaworks
2008-02-26 13:38                           ` Stephen Leake
2008-02-26 14:56                             ` adaworks
2008-02-26 19:15                               ` Ray Blaak
2008-02-26 19:13                           ` Ray Blaak
2008-02-26 21:25                             ` Ray Blaak
2008-02-27  1:15                               ` Robert A Duff
2008-02-26  7:06                       ` adaworks
2008-02-26 11:42                       ` Alex R. Mosteo
2008-02-26 15:05                         ` adaworks
2008-02-26 15:15                           ` Alex R. Mosteo
2008-02-24 17:26             ` Robert A Duff
2008-02-23 20:46         ` Robert A Duff
2008-02-24  7:31           ` adaworks
2008-02-24 17:20             ` Robert A Duff [this message]
2008-02-24 20:33               ` adaworks
2008-02-25  1:07                 ` Robert A Duff
2008-02-26  7:29                   ` adaworks
2008-02-26 19:22                     ` Ray Blaak
2008-02-27  1:58                       ` adaworks
2008-02-27 20:34                         ` Ray Blaak
2008-02-27 22:31                           ` Robert A Duff
2008-02-27 23:35                             ` Ray Blaak
2008-02-28  0:19                               ` Randy Brukardt
2008-02-28  9:18                               ` Georg Bauhaus
2008-02-29  5:57                             ` adaworks
2008-02-29  6:04                               ` Ray Blaak
2008-02-29 10:48                                 ` Alex R. Mosteo
2008-02-29 17:05                                 ` adaworks
2008-02-29 18:33                                   ` Ray Blaak
2008-02-29  6:10                           ` adaworks
2008-02-22 22:16 ` Jeffrey R. Carter
2008-02-23 13:44 ` Brian Drummond
2008-02-23 17:19   ` adaworks
2008-02-25  7:53 ` Jean-Pierre Rosen
replies disabled

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