comp.lang.ada
 help / color / mirror / Atom feed
From: rkodinets@gmail.com
Subject: Question about reference types
Date: Mon, 27 Oct 2014 05:09:48 -0700 (PDT)
Date: 2014-10-27T05:09:48-07:00	[thread overview]
Message-ID: <aa04bd67-d170-48e3-a659-239fb8acd0a9@googlegroups.com> (raw)

Hello,
I'm Ada novice and I have some trouble with understanding the Implicit_Dereference aspect feature.

I tried to use the feature with a read-only-access accessor object.

Consider the following code:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Ada_Test is
   
   type Obj is record
      A : aliased Integer;
   end record;
   
   type Obj_Access is access Obj;
   
   type Accessor (Data : access constant Integer) is null record with
     Implicit_Dereference => Data;
   
   function Get_Int (This : Obj_Access) return Accessor is
   begin
      return Accessor'(Data => This.A'Access);
   end Get_Int;
   
   X : Obj_Access := new Obj'(A => 22);
begin
   Get_Int(X).Data.all := 33; -- Error: left hand side of assignment must be a variable
   Get_Int(X) := 33;          -- No error.
   Put(X.A);                  -- This gives 33.
   New_Line;
end Ada_Test;

The RM clearly states that "A generalized_reference denotes a view equivalent to that of a dereference of the reference discriminant of the reference object". Hence, if the "reference discriminant" (Data) of the "reference object" (object of type Accessor, returned by Get_Int) has an "access constant" type and, consequently, grants only read access to the object, so should the "generalized reference", which (if I understand correcly) is Get_Int(X) inside of an assignment statement. So, I expected the compiler to print an error, just like with the normal read-only accessor. To my surprise, it compiled just fine, and the new value was assigned to the object.

Why is it so?

And one more related question. Some resourses on the Net state that usage of Implicit_Dereferense aspect make function (Get_Int in this example) overloaded. If this is the case, what is the return type of the second function? As far as I understand, it can't be Integer, because it would mean that it returns a copy of an object, and we can't change the value of the object through its copy. Is it "access Integer" which is then dereferenced implicitly? Then why it is not "access constant Integer", which would be more logical in this case? 

Thanks.


             reply	other threads:[~2014-10-27 12:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-27 12:09 rkodinets [this message]
2014-10-27 12:31 ` Question about reference types G.B.
2014-10-27 12:37   ` rkodinets
2014-10-27 22:39     ` Randy Brukardt
2014-10-28 12:33       ` Martyn Pike
2014-10-28 13:05         ` Roman Kodinets
2014-10-28 19:50           ` Martyn Pike
2014-10-28 22:53             ` Roman Kodinets
2014-10-27 18:09 ` Martyn Pike
2014-10-28 13:41 ` Roman Kodinets
2014-10-28 17:45   ` Roman Kodinets
replies disabled

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