comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: OO: How to?  Writeable from a readable?
Date: Sun, 05 Jan 2003 23:40:06 GMT
Date: 2003-01-05T23:40:06+00:00	[thread overview]
Message-ID: <qn3S9.650696$NH2.45372@sccrnsc01> (raw)
In-Reply-To: 3T2S9.721$GU4.33144@newsfep1-gui.server.ntli.net

> tagged type readable_x say, which only allows you read access to the
> data in it, and you want to extend it with a new tagged type writeable_x
> which allows you read and write access to data.  How can you do this?
>
> The problem as I see it, is that if you make the data in readable_x
> private you can't extend and modify it in the extended type, but if you
> make it public anyone can modify the data anyway.  Is there a way around
  The way (other than "constant") to make something read-only is to make
it private and supply a function that reads it and returns its value.
  If you have a type which is private, and you want operations for
another type (or any operations at all) to be able to see the private
stuff, then those operations must be either in the same package's body
(the simplest approach) or in a child package:
  package r is
    type readable is tagged private;
    function a_field(x : readable) return integer;
    function another_field(x : readable) return string;
  private
    type readable is tagged record
      a_field : integer;
      another_field : string(1 .. 10);
    end record;
  end r;
  package body r is
    function a_field(x : readable) return integer is
    begin return x.a_field;end a_field;
    function another_field(x : readable) return string is
    begin return x.another_field;end another_field;
  end r;
  package r.w is
    type writeable is new r.readable with record
      new_field : integer;
    end record;
    procedure fiddle(x : in out writeable);
  end r.w;
  package body r.w is
    procedure fiddle(x : in out writeable) is
    begin
      x.a_field := x.new_field;
    end fiddle;
  end r.w;
  with r, r.w;
  procedure testrw is
    x : r.w.writeable;
  begin
    for i in 1 .. r.w.a_field(x) loop
      r.w.fiddle(x);
    end loop;
  end testrw;



  reply	other threads:[~2003-01-05 23:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-05 23:09 OO: How to? Writeable from a readable? chris.danx
2003-01-05 23:40 ` tmoran [this message]
2003-01-06  3:39 ` John R. Strohm
2003-01-06  9:25   ` chris.danx
2003-01-06 18:00 ` OO: " Stephen Leake
replies disabled

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