comp.lang.ada
 help / color / mirror / Atom feed
* STATIC types in ADA?
@ 2002-03-01 14:27 jhuber
  2002-03-01 15:06 ` Jim Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: jhuber @ 2002-03-01 14:27 UTC (permalink / raw)


Do STATIC data types (like C++ has) exist in ADA?

By this I mean, is there a way I can define a variable so that
when I call a procedure and set a value (say 5.0) and then
exit the procedure and come back into it at a later time, the
variable would still have the same value (i.e., 5.0)?

I think private is the closest thing I've found that will do it.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STATIC types in ADA?
  2002-03-01 14:27 STATIC types in ADA? jhuber
@ 2002-03-01 15:06 ` Jim Rogers
  2002-03-01 15:33 ` Matthew Heaney
  2002-03-01 17:51 ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Jim Rogers @ 2002-03-01 15:06 UTC (permalink / raw)


In Ada you simply declare the variable in the package body,
but outside any subprogram.

Jim Rogers

jhuber@nlxcorp.ellsworth.com wrote:

> Do STATIC data types (like C++ has) exist in ADA?
> 
> By this I mean, is there a way I can define a variable so that
> when I call a procedure and set a value (say 5.0) and then
> exit the procedure and come back into it at a later time, the
> variable would still have the same value (i.e., 5.0)?
> 
> I think private is the closest thing I've found that will do it.
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STATIC types in ADA?
  2002-03-01 14:27 STATIC types in ADA? jhuber
  2002-03-01 15:06 ` Jim Rogers
@ 2002-03-01 15:33 ` Matthew Heaney
  2002-03-01 17:51 ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Matthew Heaney @ 2002-03-01 15:33 UTC (permalink / raw)



<jhuber@nlxcorp.ellsworth.com> wrote in message
news:3c7f8fd6$0$7055$43695faf@reader...
> Do STATIC data types (like C++ has) exist in ADA?
>
> By this I mean, is there a way I can define a variable so that
> when I call a procedure and set a value (say 5.0) and then
> exit the procedure and come back into it at a later time, the
> variable would still have the same value (i.e., 5.0)?

Just declare the object in a static scope (a package):

package P is
   F : Float := 0.0
end;

with P;
procedure Op is
begin
   P.F := 5.0;
end;

Realize that a variable declared in a package is equivalent to an object
declared in a C++ namespace, or as a static member of a class:

//n.hpp
namespace N
{
   extern float f;
}

//n.cpp
float N::f;

or

//c.hpp
class C
{
public:
   static float f;
//...
};

//c.cpp
float C::f;


Often variables are hidden in the body of the package, so you could write it
as:

package P is
   procedure Op;
end;

package body P is
   F : Float := 0.0;

   procedure Op is
   begin
      F := 5.0;
   end;
end P;

That would translate as:

//n.hpp
namespace N
{
   void op();
}

//n.cpp
namespace
{
   float f;
}

void N::op()
{
   f = 5;
}


or

//c.hpp
class C
{
public:
   static void op();
//...
private:
   static float f;
//...
};

//c.cpp
float C::f;

void C::op()
{
   f = 5;
}

Actually, you could declare f in an anonymous namespace in the
implementation file c.cpp, instead of as a static variable of the class.
This lets you hide it completely.  One of my complaints about C++ is that
you often have to put things in the private part of the class declaration,
up in the header file, instead of in the implementation file.  Thus in order
to get visibility, I pay with a compilation dependency.  I haven't found a
satisfactory way around this.







^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STATIC types in ADA?
  2002-03-01 14:27 STATIC types in ADA? jhuber
  2002-03-01 15:06 ` Jim Rogers
  2002-03-01 15:33 ` Matthew Heaney
@ 2002-03-01 17:51 ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2002-03-01 17:51 UTC (permalink / raw)


jhuber@nlxcorp.ellsworth.com wrote:
> 
> Do STATIC data types (like C++ has) exist in ADA?
> 
> By this I mean, is there a way I can define a variable so that
> when I call a procedure and set a value (say 5.0) and then
> exit the procedure and come back into it at a later time, the
> variable would still have the same value (i.e., 5.0)?

In Ada such a variable (not a type) is considered to contain state
information. State information is typically saved in variables in the
same package body as the subprograms that manipulate the state. In
multitasking situations (which don't arise in C++), state information is
typically saved in a protected object and manipulated by the protected
object's operations.

-- 
Jeffrey Carter



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STATIC types in ADA?
@ 2002-03-01 20:42 Gautier Write-only-address
  0 siblings, 0 replies; 5+ messages in thread
From: Gautier Write-only-address @ 2002-03-01 20:42 UTC (permalink / raw)


>Do STATIC data types (like C++ has) exist in ADA?
...
>I think private is the closest thing I've found that will do it.

It's much simpler. Ada is a block-structured language.
A variable declared outside of procedure or package P is
"static" relatively to P. Inside of P, it is a local variable
just to be used by P. Where a variable is visible, it is alife
and keep its value.

Ex:

package body P is
  i: Integer;

  procedure Q is
    j: integer;
    function R(...) return ... is
      k: integer;
    begin
      -- here i,j,k are visible
      ...
    end R;
  begin
    j:= ...
    i:= i+j;
  end Q;

begin
  i:= 0;
  Q;
  -- i has changed
  Q;
  -- again...
end P;

________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, address on the Web site!



_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-03-01 20:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-01 14:27 STATIC types in ADA? jhuber
2002-03-01 15:06 ` Jim Rogers
2002-03-01 15:33 ` Matthew Heaney
2002-03-01 17:51 ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2002-03-01 20:42 Gautier Write-only-address

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