comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Is Ada.Containers.Vectors.Reference_Type a constrained view?
Date: Sat, 28 Oct 2017 04:10:45 -0700 (PDT)
Date: 2017-10-28T04:10:45-07:00	[thread overview]
Message-ID: <fad2c15c-52e5-416a-b133-a3674afc318c@googlegroups.com> (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


             reply	other threads:[~2017-10-28 11:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-28 11:10 Stephen Leake [this message]
2017-11-15  0:38 ` Is Ada.Containers.Vectors.Reference_Type a constrained view? 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