comp.lang.ada
 help / color / mirror / Atom feed
From: martinbbjerregaard@gmail.com
Subject: Mutating elements of constant list using a container element iterator
Date: Tue, 4 Aug 2015 16:56:25 -0700 (PDT)
Date: 2015-08-04T16:56:25-07:00	[thread overview]
Message-ID: <0c1904af-8ede-4694-a50a-ac9f60d7dc63@googlegroups.com> (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?


             reply	other threads:[~2015-08-04 23:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-04 23:56 martinbbjerregaard [this message]
2015-08-05  7:32 ` Mutating elements of constant list using a container element iterator 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
replies disabled

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