comp.lang.ada
 help / color / mirror / Atom feed
* Mutating elements of constant list using a container element iterator
@ 2015-08-04 23:56 martinbbjerregaard
  2015-08-05  7:32 ` Egil H H
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: martinbbjerregaard @ 2015-08-04 23:56 UTC (permalink / raw)


Mutating elements of a constant container in a "for ... of ... loop" is possible with the "standard" container types but not the indefinite or bounded containers:

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Indefinite_Doubly_Linked_Lists;

procedure Strange_Behavior is
   
   type Test is tagged
      record
         Value : Integer;
      end record;
   
   package Test_Lists is new Ada.Containers.Doubly_Linked_Lists (Element_Type => Test);
   package Test_Indefinite_Lists is new Ada.Containers.Indefinite_Doubly_Linked_Lists (Element_Type => Test);
   
   function Make_List return Test_Lists.List is
   begin
      return Result : Test_Lists.List := Test_Lists.Empty_List do
         for I in 0 .. 100 loop
            Result.Append (Test'(Value => I));
         end loop;
      end return;
   end Make_List;
   
   function Make_Indefinite_List return Test_Indefinite_Lists.List is
   begin
      return Result : Test_Indefinite_Lists.List := Test_Indefinite_Lists.Empty_List do
         for I in 0 .. 100 loop
            Result.Append (Test'(Value => I));
         end loop;
      end return;
   end Make_Indefinite_List;
   
   V1 : constant Test_Lists.List := Make_List;
   V2 : constant Test_Indefinite_Lists.List := Make_Indefinite_List;
   
begin
   
   for Item of V1 loop
      Item.Value := 0; -- no error
   end loop;
   
   for Item of V2 loop
      Item.Value := 0; -- error: "left hand side of assignment must be a variable"
   end loop;
   
end Strange_Behavior;

http://www.ada-auth.org/standards/12rat/html/Rat12-8-3.html says:
"If we write
for E of The_List loop
   ...   -- do something to Element E
end loop;
then we can change the element E unless The_List has been declared as constant."

Is this a bug?


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

* Re: Mutating elements of constant list using a container element iterator
  2015-08-04 23:56 Mutating elements of constant list using a container element iterator martinbbjerregaard
@ 2015-08-05  7:32 ` Egil H H
  2015-08-05 11:23   ` martinbbjerregaard
  2015-08-05  7:43 ` Jacob Sparre Andersen
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Egil H H @ 2015-08-05  7:32 UTC (permalink / raw)


On Wednesday, August 5, 2015 at 1:56:26 AM UTC+2, martinbbj...@gmail.com wrote:
>    
>    for Item of V1 loop
>       Item.Value := 0; -- no error
>    end loop;

Gnat Pro 7.3.1 reports "left hand side of assignment must be a variable" here.

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

* Re: Mutating elements of constant list using a container element iterator
  2015-08-04 23:56 Mutating elements of constant list using a container element iterator martinbbjerregaard
  2015-08-05  7:32 ` Egil H H
@ 2015-08-05  7:43 ` Jacob Sparre Andersen
  2015-08-05 11:35 ` martinbbjerregaard
  2015-08-06 19:02 ` Randy Brukardt
  3 siblings, 0 replies; 6+ messages in thread
From: Jacob Sparre Andersen @ 2015-08-05  7:43 UTC (permalink / raw)


martinbbjerregaard@gmail.com writes:

> Mutating elements of a constant container in a "for ... of ... loop"
> is possible with the "standard" container types but not the indefinite
> or bounded containers:

The trick is to assign a new value to the whole object, and not just a
component of it:

   for Item of V2 loop
      Item := (Value => 0);
   end loop;

I'm not sure exactly why there is a difference.

> Is this a bug?

It might be.  I don't understand the indefinite containers well enough to
be able to explain what happens.

Greetings,

Jacob
-- 
"The problem with America is stupidity. I'm not saying there
 should be a capital punishment for stupidity, but why don't
 we just take the safety labels off of everything and let
 the problem solve itself?"


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

* Re: Mutating elements of constant list using a container element iterator
  2015-08-05  7:32 ` Egil H H
@ 2015-08-05 11:23   ` martinbbjerregaard
  0 siblings, 0 replies; 6+ messages in thread
From: martinbbjerregaard @ 2015-08-05 11:23 UTC (permalink / raw)


> On Wednesday, August 5, 2015 at 1:56:26 AM UTC+2, martinbbj...@gmail.com wrote:
> >    
> >    for Item of V1 loop
> >       Item.Value := 0; -- no error
> >    end loop;
> 
> Gnat Pro 7.3.1 reports "left hand side of assignment must be a variable" here.

I forgot to mention I'm using GNAT GPL 2015 (20150428-49)

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

* Re: Mutating elements of constant list using a container element iterator
  2015-08-04 23:56 Mutating elements of constant list using a container element iterator martinbbjerregaard
  2015-08-05  7:32 ` Egil H H
  2015-08-05  7:43 ` Jacob Sparre Andersen
@ 2015-08-05 11:35 ` martinbbjerregaard
  2015-08-06 19:02 ` Randy Brukardt
  3 siblings, 0 replies; 6+ messages in thread
From: martinbbjerregaard @ 2015-08-05 11:35 UTC (permalink / raw)


Egil H H writes:
> Gnat Pro 7.3.1 reports "left hand side of assignment must be a variable" here.

That is strange. I'm using GNAT GPL 2015 (20150428-49), so I'm going to assume that it is a bug then.

Jacob Sparre Andersen writes:
> The trick is to assign a new value to the whole object, and not just a
> component of it:
> 
>    for Item of V2 loop
>       Item := (Value => 0);
>    end loop;
> 
> I'm not sure exactly why there is a difference.

for Item of V1 loop
   Item := (Value => 0); -- still no error
end loop;

This still gives no error, even though V1 is declared as constant.


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

* Re: Mutating elements of constant list using a container element iterator
  2015-08-04 23:56 Mutating elements of constant list using a container element iterator martinbbjerregaard
                   ` (2 preceding siblings ...)
  2015-08-05 11:35 ` martinbbjerregaard
@ 2015-08-06 19:02 ` Randy Brukardt
  3 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2015-08-06 19:02 UTC (permalink / raw)


<martinbbjerregaard@gmail.com> wrote in message 
news:0c1904af-8ede-4694-a50a-ac9f60d7dc63@googlegroups.com...
...
> http://www.ada-auth.org/standards/12rat/html/Rat12-8-3.html says:
> "If we write
> for E of The_List loop
>   ...   -- do something to Element E
> end loop;
> then we can change the element E unless The_List has been declared as 
> constant."
>
> Is this a bug?

Definitely a bug. There is an ACATS test for this that was created in June 
2014 and corrected in July 2014; presumably GNAT was fixed sometime after 
that. That would explain why GNATPro properly detects the error, probably 
GNAT GPL was split off sooner than the fix.

                                            Randy.




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

end of thread, other threads:[~2015-08-06 19:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-04 23:56 Mutating elements of constant list using a container element iterator martinbbjerregaard
2015-08-05  7:32 ` Egil H H
2015-08-05 11:23   ` martinbbjerregaard
2015-08-05  7:43 ` Jacob Sparre Andersen
2015-08-05 11:35 ` martinbbjerregaard
2015-08-06 19:02 ` Randy Brukardt

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