comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Is Ada.Containers.Vectors.Reference_Type a constrained view?
Date: Tue, 14 Nov 2017 18:38:28 -0600
Date: 2017-11-14T18:38:28-06:00	[thread overview]
Message-ID: <oug2a4$hnm$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: fad2c15c-52e5-416a-b133-a3674afc318c@googlegroups.com

"Stephen Leake" <stephen_leake@stephe-leake.org> wrote in message 
news:fad2c15c-52e5-416a-b133-a3674afc318c@googlegroups.com...
>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'.

Of course it is an access-to-object type; the only alternative is an 
access-to-subprogram type, and it surely isn't that.

But your question really is, is the object designated by the reference 
object allowed to be constrained (that is, allowed to be an allocated 
object). This doesn't seem to be answered by the RM.

The question IS answered for Update_Element (see A.18.2(142/2)).

I'd argue that the same rule is intended for Reference_Type, but since there 
is no wording requiring it in the RM, it's hard to say that the GNAT 
implementation is wrong. You might try two things:
(1) Try using Update_Element rather than Reference. (A pain, I know.) If 
that does not work either, then GNAT is wrong, file a bug report.
(2) Send a question to Ada-Comment so that the ARG considers the question. I 
don't think we ever intended Update_Element and Reference to work 
differently, but it ought to be discussed.

Note that the answer would be different had you been using 
Indefinite_Vectors. (Those definitely allow the elements to be constrained, 
as those pretty much have to be allocated individually when they are 
created.)

                                         Randy.





> 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-11-15  0:38 UTC|newest]

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

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