[comp.lang.ada] Readonly variables in Ada?

collberg@dna.lth.se (Christian S. Collberg) (06/05/90)

Is it possible to declare (the equivalent of) an exported readonly
variable in Ada? I'm referring to something similar to the READ_ONLY
attribute present in Mesa. I'm having a hard time reading the 
appropriate sections in the LRM, and I don't have a compiler at hand
to try it out, so maybe someone out there could enlighten me...

Maybe something along these lines might work (excuse the syntax, I'm not an
Ada programmer):

 ---------------------------
   package P;
       type T is limited private;
       v : T;            -- We want a client of P to be able to
                         -- perform any operation on v, except
                         -- assignment.
   private
      type T is BOOLEAN;
   end P;
 ---------------------------

 ---------------------------
   with P;
   declare
      x : BOOLEAN;
   begin 
      P.v := false;       -- Obviously illegal, as it should be 
                          -- for a read only variable.

      if P.v = true then  -- Doesn't seem like this would be 
          ...             -- legal since equality is undefined
                          -- for limited private types.
          

      if P.v then         -- legal ?
         ...

      if P.v OR x then    -- legal ?
         ...

   end;
---------------------------


Would one, in order to achieve the desired effect, have to provide,
in the interface of P, overloaded operators for the type T (=, OR, AND,
etc for this example)?

Thanks in advance for any help you may offer in clearing this up for me.

Chris Collberg
collberg@dna.lth.se

progers@ajpo.sei.cmu.edu (Pat Rogers) (06/05/90)

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? 

The best way I know is to use a discriminated record that is exported as
a private/limited type.  The discriminates are visible and thus readable,
but since the type is actually private (or limited), they cannot be written
to from outside the package's exported subprograms.  You may also have
internal variables (components of the exported record type) that cannot
be seen at all, if you choose, or simply make the record empty via "null".

For example:

(This hasn't "experienced the joys of compilation" as a friend of mine says,
but I have done it before, so to speak, and this is the general approach.)

package Hardware is

  type Register( B0,B1,B2,B3,B4,B5,B6,B7 : Integer := 0 ) is private;  

  procedure Set( R : in out Register; ...... );

  -- other procedures and functions which operate on registers
private

  type Register( <as before> ) is
    record
      null;  -- or internal, hidden components
    end record;

end Hardware;

with Hardware;
package Machine is

  Status : Hardware.Register;

  ...

end Machine;

From here, Machine.Status.B0 etc can be read, but not written, since the
type is private.  Is that close to what you need?

Regards,
P Rogers
Software Arts and Sciences

collard@software.org (David Collard) (06/05/90)

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? I'm referring to something similar to the READ_ONLY
> attribute present in Mesa. I'm having a hard time reading the 

>    package P;
>        type T is limited private;
>        v : T;            -- We want a client of P to be able to
>                          -- perform any operation on v, except
>                          -- assignment.
>    private
>       type T is BOOLEAN;
>    end P;

I think, for a read only VARIABLE, there is an easier answer -- the type
can be visible, but declare the variable in the body of the package and
only provide an interface to read it.

package P is
  function T return Boolean;
end P;

package body P is
  Static_Package_Variable : Boolean;
  function T return Boolean is
  begin
    return Static_Package_Variable;
  end T;
end P;

with P;
procedure P_User is
begin
  if P.T then  --Legal
    null;
  end if;

  P.Static_Package_Variable := False; -- Illegal because the variable is not
                                         visible.

end P_User;
A
end P_User;


--

-----------------------------------------------------------------------
D. Thor Collard                      Internet: collard@software.org
Software Productivity Consortium     UUNET:    ...!uunet!software!collard
2214 Rock Hill Rd, Herndon VA 22070  

eachus@linus.mitre.org (Robert I. Eachus) (06/06/90)

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;


  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:

  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.