From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!watserv1!watmath!att!rutgers!cs.utexas.edu!sdd.hp.com!think!linus!eachus From: eachus@linus.mitre.org (Robert I. Eachus) Newsgroups: comp.lang.ada Subject: Re: Readonly variables in Ada? Message-ID: Date: 5 Jun 90 18:01:25 GMT References: <1990Jun5.085654.920@lth.se> Sender: usenet@linus.mitre.org Organization: The Mitre Corporation, Bedford, MA In-reply-to: collberg@dna.lth.se's message of 5 Jun 90 08:56:54 GMT List-Id: 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.