TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Mime-Version: 1.0
Sender: "Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Date: Fri, 4 Dec 1998 08:19:11 -0800
Reply-To: Mark Lundquist <[log in to unmask]>
From: Mark Lundquist <[log in to unmask]>
In-Reply-To: Message from Igor Izvarin <[log in to unmask]> of "Fri, 04 Dec 1998 17:48:16 +0300." <[log in to unmask]>
Content-Type: text/plain; charset=us-ascii
Parts/Attachments: text/plain (73 lines)
From:  Igor Izvarin <[log in to unmask]>

> Hello everybody.
> I am novice in Ada programming and just came from C/C++. Sorry I could not
> find another discussion forum on Internet more technical so I am asking
> question here.
> I translate my source code from C/C++ and meet one feature I can not
> resolve: the static variables in Ada.
> The example:
> In C:
> Int foo()
> {
> static int v;
> v = init_static();
> ...
> }
> and the v variable will hold the value between calls. How can I do this in
> Ada?

Is that a good example?  (the value of v is assigned again anyway by
the call to init_static() ...?)

In any case... I think

    package body Outer is

        package Inner is
            function Foo return Integer;
        end Inner;

        package body Inner is

            V : Integer;        -- V is visible within the body of
                                -- Inner, e.g. to Foo

            function Foo return Integer is

        end Inner;

        function Foo renames Inner.Foo;

    end Outer; you what you want.  Here, Inner exists just to hide V.  If you
added more things to Inner, they could also see V; this would then be
wider visibility than a function static variable.

If you don't care about hiding V to other things in the body of Outer,
you can dispense with Inner (and the rename-as-body) altogether:

    package body Outer is

        V : Integer;

        function Foo return Integer is

        end Inner;

    end Outer;