comp.lang.ada
 help / color / mirror / Atom feed
* Question about reference types
@ 2014-10-27 12:09 rkodinets
  2014-10-27 12:31 ` G.B.
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: rkodinets @ 2014-10-27 12:09 UTC (permalink / 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.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-27 12:09 Question about reference types rkodinets
@ 2014-10-27 12:31 ` G.B.
  2014-10-27 12:37   ` rkodinets
  2014-10-27 18:09 ` Martyn Pike
  2014-10-28 13:41 ` Roman Kodinets
  2 siblings, 1 reply; 11+ messages in thread
From: G.B. @ 2014-10-27 12:31 UTC (permalink / raw)


On 27.10.14 13:09, rkodinets@gmail.com wrote:
> 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.
>     Get_Int(X).Data.all := 33; -- Error: left hand side of assignment must be a variable
>     Get_Int(X) := 33;          -- No error.


> Why is it so?

To me it looks like an effect of trying new features not
yet widely supported, or tested. But certainly worth a report
if confirmed.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-27 12:31 ` G.B.
@ 2014-10-27 12:37   ` rkodinets
  2014-10-27 22:39     ` Randy Brukardt
  0 siblings, 1 reply; 11+ messages in thread
From: rkodinets @ 2014-10-27 12:37 UTC (permalink / raw)


On Monday, October 27, 2014 3:31:52 PM UTC+3, G.B. wrote:
> To me it looks like an effect of trying new features not
> yet widely supported, or tested. But certainly worth a report
> if confirmed.

I tested it with GNAT GPL 2014 and GNAT FSF 4.8.2 with same result. (Forgot to mention it in OP)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-27 12:09 Question about reference types rkodinets
  2014-10-27 12:31 ` G.B.
@ 2014-10-27 18:09 ` Martyn Pike
  2014-10-28 13:41 ` Roman Kodinets
  2 siblings, 0 replies; 11+ messages in thread
From: Martyn Pike @ 2014-10-27 18:09 UTC (permalink / raw)


On 27/10/2014 12:09, rkodinets@gmail.com wrote:
> 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

This is where the error is.  Data is constant.

>       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;


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-27 12:37   ` rkodinets
@ 2014-10-27 22:39     ` Randy Brukardt
  2014-10-28 12:33       ` Martyn Pike
  0 siblings, 1 reply; 11+ messages in thread
From: Randy Brukardt @ 2014-10-27 22:39 UTC (permalink / raw)


Looks like a GNAT bug to me. Send them a bug report.

And don't make the mistake of thinking that what GNAT does is the same as 
what Ada requires. :-) All complex software has bugs, and GNAT surely is no 
exception.

                                  Randy.

<rkodinets@gmail.com> wrote in message 
news:7804a038-2eb0-4d70-b4ad-9bca94888661@googlegroups.com...
> On Monday, October 27, 2014 3:31:52 PM UTC+3, G.B. wrote:
>> To me it looks like an effect of trying new features not
>> yet widely supported, or tested. But certainly worth a report
>> if confirmed.
>
> I tested it with GNAT GPL 2014 and GNAT FSF 4.8.2 with same result. 
> (Forgot to mention it in OP) 




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-27 22:39     ` Randy Brukardt
@ 2014-10-28 12:33       ` Martyn Pike
  2014-10-28 13:05         ` Roman Kodinets
  0 siblings, 1 reply; 11+ messages in thread
From: Martyn Pike @ 2014-10-28 12:33 UTC (permalink / raw)


On 27/10/2014 22:39, Randy Brukardt wrote:
> Looks like a GNAT bug to me. Send them a bug report.
>
> And don't make the mistake of thinking that what GNAT does is the same as
> what Ada requires. :-) All complex software has bugs, and GNAT surely is no
> exception.
>
>                                    Randy.
>
> <rkodinets@gmail.com> wrote in message
> news:7804a038-2eb0-4d70-b4ad-9bca94888661@googlegroups.com...
>> On Monday, October 27, 2014 3:31:52 PM UTC+3, G.B. wrote:
>>> To me it looks like an effect of trying new features not
>>> yet widely supported, or tested. But certainly worth a report
>>> if confirmed.
>>
>> I tested it with GNAT GPL 2014 and GNAT FSF 4.8.2 with same result.
>> (Forgot to mention it in OP)
>
>

I don't think this is a bug.

	type Accessor (Data : access constant Integer) with

Means Data is access to a constant Integer.  The OP is trying to modify 
Data with :

	Get_Int(X).Data.all := 33;

Martyn



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-28 12:33       ` Martyn Pike
@ 2014-10-28 13:05         ` Roman Kodinets
  2014-10-28 19:50           ` Martyn Pike
  0 siblings, 1 reply; 11+ messages in thread
From: Roman Kodinets @ 2014-10-28 13:05 UTC (permalink / raw)


On Tuesday, October 28, 2014 3:36:20 PM UTC+3, Martyn Pike wrote:
> I don't think this is a bug.
> 
> 	type Accessor (Data : access constant Integer) with
> 
> Means Data is access to a constant Integer.  The OP is trying to modify 
> Data with :
> 
> 	Get_Int(X).Data.all := 33;
> 
> Martyn

Yes, the error message is not a bug. The (possible) bug is that there is no error message being triggered by the next line
Get_Int(X) := 33;
which should be semantically the same thing as
Get_Int(X).Data.all := 33;

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-27 12:09 Question about reference types rkodinets
  2014-10-27 12:31 ` G.B.
  2014-10-27 18:09 ` Martyn Pike
@ 2014-10-28 13:41 ` Roman Kodinets
  2014-10-28 17:45   ` Roman Kodinets
  2 siblings, 1 reply; 11+ messages in thread
From: Roman Kodinets @ 2014-10-28 13:41 UTC (permalink / raw)


I've sent the bug report to AdaCore.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-28 13:41 ` Roman Kodinets
@ 2014-10-28 17:45   ` Roman Kodinets
  0 siblings, 0 replies; 11+ messages in thread
From: Roman Kodinets @ 2014-10-28 17:45 UTC (permalink / raw)


I received the reply (already!), they say it works properly in the development version of GNAT. So, it is solved now.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-28 13:05         ` Roman Kodinets
@ 2014-10-28 19:50           ` Martyn Pike
  2014-10-28 22:53             ` Roman Kodinets
  0 siblings, 1 reply; 11+ messages in thread
From: Martyn Pike @ 2014-10-28 19:50 UTC (permalink / raw)


On 28/10/2014 13:05, Roman Kodinets wrote:
> On Tuesday, October 28, 2014 3:36:20 PM UTC+3, Martyn Pike wrote:
>> I don't think this is a bug.
>>
>> 	type Accessor (Data : access constant Integer) with
>>
>> Means Data is access to a constant Integer.  The OP is trying to modify
>> Data with :
>>
>> 	Get_Int(X).Data.all := 33;
>>
>> Martyn
>
> Yes, the error message is not a bug. The (possible) bug is that there is no error message being triggered by the next line
> Get_Int(X) := 33;
> which should be semantically the same thing as
> Get_Int(X).Data.all := 33;
>

Agreed. I should have read all of your original post :-(


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Question about reference types
  2014-10-28 19:50           ` Martyn Pike
@ 2014-10-28 22:53             ` Roman Kodinets
  0 siblings, 0 replies; 11+ messages in thread
From: Roman Kodinets @ 2014-10-28 22:53 UTC (permalink / raw)


On Tuesday, October 28, 2014 10:59:45 PM UTC+3, Martyn Pike wrote:
> On 28/10/2014 13:05, Roman Kodinets wrote:
> > On Tuesday, October 28, 2014 3:36:20 PM UTC+3, Martyn Pike wrote:
> >> I don't think this is a bug.
> >>
> >> 	type Accessor (Data : access constant Integer) with
> >>
> >> Means Data is access to a constant Integer.  The OP is trying to modify
> >> Data with :
> >>
> >> 	Get_Int(X).Data.all := 33;
> >>
> >> Martyn
> >
> > Yes, the error message is not a bug. The (possible) bug is that there is no error message being triggered by the next line
> > Get_Int(X) := 33;
> > which should be semantically the same thing as
> > Get_Int(X).Data.all := 33;
> >
> 
> Agreed. I should have read all of your original post :-(

My bad, actually. The comments in the example were a little misleading. I should have wrote something like "Error here, and this is ok." and "No error here, why?" (actually, I thought of it the second after I've sent the post).


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-10-28 22:53 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-27 12:09 Question about reference types rkodinets
2014-10-27 12:31 ` 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

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