comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@linus.mitre.org (Robert I. Eachus)
Subject: Re: Readonly variables in Ada?
Date: 5 Jun 90 18:01:25 GMT	[thread overview]
Message-ID: <EACHUS.90Jun5140125@aries.linus.mitre.org> (raw)
In-Reply-To: collberg@dna.lth.se's message of 5 Jun 90 08:56:54 GMT

In article <1990Jun5.085654.920@lth.se> collberg@dna.lth.se (Christian S. Collberg) writes:

>  Is it possible to declare (the equivalent of) an exported readonly
>  variable in Ada? 

    If what you want is a value which can is not writable except
within the defining package, the usual Ada style is to export a
function.  For this example it would be easy to make V of type Boolean
and have all the functionality you seem to want, but the private type
example is also interesting:

  package P;
    type T is private;
    function V  return T;   -- We want a client of P to be able to
			    -- perform any operation on v, except
			    -- assignment.
    function True return T;
    function False return T;
    -- See if you can figure out how to write the body of these
    -- functions!
    pragma INLINE (V, True, False);
  private
    type T is new BOOLEAN;
  end P;

\f
  with P;
  ...
  declare
    X : T;
  begin 
    P.V := false;       -- Obviously illegal, as it should be 
		        -- for a read only variable.
    if P.V = true then  -- Legal

    if P.V then         -- Illegal outside of P (V is not of a boolean type.)
    ...
    if P.v OR x then    -- Legal only if OR has been overloaded... 
    ...
  end;

   For completeness the body of package P is:
\f
  package P;
  -- type T is private;
    Hidden_V:T := T'FIRST;

    function True return T is begin return T'LAST; end;
    function False return T is begin return T'FIRST; end;
  -- That was easy wasn't it...
    function V  return T is return Hidden_V; end;
  ...
  end P;
--

					Robert I. Eachus

   Amiga 3000 - The hardware makes it great, the software makes it
                awesome, and the price will make it ubiquitous.

      parent reply	other threads:[~1990-06-05 18:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1990-06-05  8:56 Readonly variables in Ada? Christian S. Collberg
1990-06-05 13:21 ` Pat Rogers
1990-06-05 16:22 ` David Collard
1990-06-05 18:01 ` Robert I. Eachus [this message]
replies disabled

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