comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: local variables
Date: 1998/04/10
Date: 1998-04-10T00:00:00+00:00	[thread overview]
Message-ID: <dewar.892262465@merv> (raw)
In-Reply-To: Er7o0u.43t.0.-s@inmet.camb.inmet.com


Tucker said

<<  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;
>>

You really don't need the relaxed rules of Ada 95 here, and indeed they
are not really sufficient in this case (what if there were another
routine after Bump_Counter, it would be able to see Counter, which is
wrong).

Instead you can just introduce a nested package:

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

    package body Encapsulate is

      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 Encapsulate;
  end Abstraction;


The corresponding package spec might have

  package Abstraction is
    procedure Op1(...);

    package Encapsulate is
      procedure Bump_Counter (Next : out Integer);
    end Encapsulate;

    procedure Bump_Counter (Next : out Integer)
      renames Encapsulate.Bump_Counter;

  end Abstraction;

and this can of course be done in Ada 83, so the reference to this as a
crippling problem seems overblown!





  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 ` Robert Dewar
1998-04-10  0:00 ` Simon Wright
1998-04-10  0:00 ` Tucker Taft
1998-04-10  0:00   ` Robert Dewar [this message]
1998-04-13  0:00     ` Fergus Henderson
1998-04-10  0:00 ` Niklas Holsti
replies disabled

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