comp.lang.ada
 help / color / mirror / Atom feed
From: hreba <hreba@terra.com.br>
Subject: Re: How to use read-only variables?
Date: Sun, 04 Jan 2015 10:56:38 -0200
Date: 2015-01-04T10:56:38-02:00	[thread overview]
Message-ID: <cgsrk8FarkmU1@mid.individual.net> (raw)
In-Reply-To: <cgrhh9F1465U1@mid.individual.net>

On 01/03/2015 10:58 PM, hreba wrote:
> My Ada book says that 'access const' types serve for readonly
> variables but I am not sure whether I grasped the idea.
>
> I want a readonly variable 'n' as component of a record 'R'. The
> variable which is accessed by 'n', let's call it 'nv' must be hidden,
> otherwise the client could change it.
>
> Consequently 'R' must be extensible, that is tagged. It makes no sense
> to use the base class alone (without something for 'n' to point to),
> so it must be abstract. As the pointer-target relation wouldn't be
> conserved in a simple assignment, the record must be limited. This
> leads to
>
>     type R is abstract tagged limited record
>        n:    access constant Integer;
>     end record;
>
>     type Rv is new R with private;
>
> private
>
>     type Rv is new R with record
>        nv:    aliased Integer;
>     end record;
>
> The value of 'n' would be defined in an initialization procedure:
>
>     procedure Init (rec: in out Rv) is
>     begin
>        rec.n:= rec.nv'Access;
>     end Init;
>
> This however gives me an error message:
>
>     non-local pointer cannot point to local object
>
> Now I am really confused:
>
>   - Are readonly variables really handled this way, typically?
>   - How can rec.nv be local and rec.n not?
>   - How would you write the correct initialization procedure?
>
> All this seems pretty complicated to somebody used to Oberon, where
> all this would be simply written as
>
> TYPE
>     R* = RECORD
>             n-: INTEGER;
>          END;
>

I am responding to my own post in order not to repeat myself in 
individual answers.

First of all: thanks for your help.

Second, I thought the meaning of "read-only" was clear, but perhaps it 
isn't. What I meant is what Brad expressed as "read only from the client 
perspective but modifiable locally".

Third, looking at the answer of Shark8 which solves the problem using 
"access const", I guess using a function returning the value of a hidden 
variable would be a much simpler solution. An example of a read-only 
variable would be the number of elements in a linked list. Within the 
defining package it is changed whenever the user adds or removes an 
element, you want the user to read it, but by no means you want to give 
him write access to it. I guess I understand now why 'Length' in 
Ada.Containers.Doubly_Linked_Lists is a function and no read-only variable.

Fourth, in what practical cases do you really use 'access const' for 
read-only variables; I thought my case a pretty typical one?

-- 
Frank Hrebabetzky		+55 / 48 / 3235 1106
Florianopolis, Brazil

  parent reply	other threads:[~2015-01-04 12:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-04  0:58 How to use read-only variables? hreba
2015-01-04  1:50 ` Hubert
2015-01-04  2:04 ` Hubert
2015-01-04  2:33 ` Brad Moore
2015-01-04  2:55 ` Shark8
2015-01-04 12:56 ` hreba [this message]
2015-01-04 15:34   ` sbelmont700
2015-01-04 20:45   ` Brad Moore
2015-01-04 20:48     ` Brad Moore
replies disabled

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