comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: local variables
Date: 1998/04/10
Date: 1998-04-10T00:00:00+00:00	[thread overview]
Message-ID: <Er7o0u.43t.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 352E491F.4C6D@young.epc.lmms.lmco.com


B.Voh (voh@young.epc.lmms.lmco.com) wrote:

: Does new ADA support a retention of local variables similar to Fortran's
: "save" or C's "static" declaration?

No.  However, the "new" Ada (aka Ada 95) does allow variable declarations
to be interleaved with subprogram bodies in a package body, so it
is easier to localize package-level variables with a subprogram that 
needs one.  In my experience, however, it is quite common that
there are several subprograms in a given abstraction that all need
access to the "static" variables retaining state across calls.
So moving such variables to the package level usually makes
good sense anyway.

: In my last encounter with ADA, some years back, I found that to be a
: rather crippling ommission and often working against the very principles
: (encapsulation) it was supposed to champion.

I would be curious in what way this is crippling.  All declarations
in a package body are "encapsulated," including package-level
variables used to retain state between calls of a given subprogram.

For what it is worth, Java also omits the notion of "local static" variables.

Here is an example which takes advantage of the Ada 95 relaxed ordering
for variable declarations and subprogram bodies, to allow package-level
variables to be grouped with associated subprograms:

  package Abstraction is
    procedure Op1(...);
    procedure Bump_Counter(Next : out Integer);
  end Abstraction

  package body Abstraction is
    procedure Op1(...) is
    begin
      -- do something interesting
    end Op1;

    Counter : Integer := 0;  -- "static" variable used by Bump_Counter
    procedure Bump_Counter(Next : out Integer) is
    begin
      Counter := Counter + 1;
      Next := Counter;
    end Bump_Counter;
  end Abstraction;

In any case, "Counter" is not visible outside the Abstraction package,
and so it is still "encapsulated."

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




  reply	other threads:[~1998-04-10  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-04-10  0:00 local variables B.Voh
1998-04-10  0:00 ` Tucker Taft [this message]
1998-04-10  0:00   ` Robert Dewar
1998-04-13  0:00     ` Fergus Henderson
1998-04-10  0:00 ` Niklas Holsti
1998-04-10  0:00 ` Simon Wright
1998-04-10  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