comp.lang.ada
 help / color / mirror / Atom feed
* Is Ada.Containers.Vectors.Reference_Type a constrained view?
@ 2017-10-28 11:10 Stephen Leake
  2017-11-15  0:38 ` Randy Brukardt
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Leake @ 2017-10-28 11:10 UTC (permalink / raw)


I have the following vector type:

   type Indent_Labels is (Not_Set, Int, Anchor, Anchored, Nested_Anchor);

   type Indent_Type (Label : Indent_Labels := Not_Set) is record
      case Label is
      when Not_Set =>
         null;

      when Int => 
         Int_Offset : Integer;

      ...

      end case;
   end record;

   package Indent_Vectors is new Ada.Containers.Vectors 
     (Line_Number_Type, Indent_Type);

Then I have code like this:

      for Line in First_Line .. Last_Line loop
         declare
            Indent : Indent_Type := Data.Indents (Line);
         begin
            case Delta_Indent.Label is
            when Int =>
               Indent_Apply_Int (Indent, Delta_Indent.Offset);
            ...
            end case;

            Data.Indents.Replace_Element (Line, Indent);
         end;
      end loop;

The body of Indent_Apply_Int may change the Label from Not_Set to Int; the other branches of the case statement change Not_Set to the other label values.

I'd like to avoid copying Indent (mostly on general principle; this is not a large object). However, if I use:

   Indent : Indent_Type renames Data.Indents.Reference (Line).Element.all;

and drop the Replace_Element call, then Constraint_Error is raised at the point in Indent_Apply_Int that changes the Label (I'm using GNAT GPL 2017).

I'm not clear if that Constraint_Error is allowed by the ARM. Reference returns a Reference_Type object:

   type Reference_Type (Element : not null access Element_Type) is private
   with
      Implicit_Dereference => Element;

Note that Element is of an access type. AARM 4.8 (6) says that allocated objects are constrained by their initial value. AARM 3.10 (26.d/2) says most non-allocated objects accessed via an access-to-object type are not constrained.

So is the type of discriminant Element an access to object type? It doesn't have 'all' in it, so I guess not. Note that the syntax of discriminants does not allow 'all'.

This seems to be a flaw; it would be nice to be able to use Reference in the code above, especially if the copy operation is slow. I guess I could use Update_Element in that case.

In Ada.Containers.Indefinite_Vectors, the Element parameter of Update_Element is allowed to be constrained (AARM A.18.11 (8/2)); that statement is not made in Ada.Containers.Vectors, so the actual Element in a Vector cannot be constrained.

-- Stephe


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

end of thread, other threads:[~2017-11-15  0:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-28 11:10 Is Ada.Containers.Vectors.Reference_Type a constrained view? Stephen Leake
2017-11-15  0:38 ` Randy Brukardt

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