comp.lang.ada
 help / color / mirror / Atom feed
From: hreba <hreba@terra.com.br>
Subject: How to use read-only variables?
Date: Sat, 03 Jan 2015 22:58:15 -0200
Date: 2015-01-03T22:58:15-02:00	[thread overview]
Message-ID: <cgrhh9F1465U1@mid.individual.net> (raw)

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;

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


             reply	other threads:[~2015-01-04  0:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-04  0:58 hreba [this message]
2015-01-04  1:50 ` How to use read-only variables? 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
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