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

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