comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Empty Bounded Containers
Date: Thu, 22 May 2014 18:05:19 -0700 (PDT)
Date: 2014-05-22T18:05:19-07:00	[thread overview]
Message-ID: <c7ae9817-f4de-4159-8cd9-b920c7641492@googlegroups.com> (raw)
In-Reply-To: <cfe5082c-9b50-4c5b-afe1-8dd6ea1640b9@googlegroups.com>

On Thursday, May 22, 2014 5:20:51 PM UTC-7, sbelm...@gmail.com wrote:
> Hi,
> 
> In the sundry Ada distributions I have around, the "empty" bounded containers are fully defined as having a capacity of zero, e.g.
> 
> Empty_Vector : constant Vector := (Capacity => 0, others => <>);
> 
> which is not intuitively what I would assume.  For instance, the following line:
> 
> x : BC.Vector (Capacity => 42) := BC.Empty_Vector;
> 
> implies an container with a capacity of 42 initialized to a length of zero, but it of course throws a constraint error.  In fact, there really doesn't seem to be any valid use of the Empty_Vector (et al), since the actual capacity is hidden in the private part.

I don't understand this.  If you write a procedure that takes a Vector as input, it is often very handy to give callers the ability to call your procedure with Empty_Vector as the actual parameter.

>  The LRM just says it should be the same between the two without addressing the discriminant, which isn't much help.  
> 
> Moreover, the wording seems to legally contradict itself, since by stating "If an object of type Vector is not otherwise initialized, it is initialized to the same value as Empty_Vector" it would seem to imply that 
> 
> x : BC.Vector (Capacity => 42);
> 
> ought to use the values from the Empty_Container, causing the same error, leaving no actual way to ever have any bounded containers at all.
> 
> Does anyone know what the intention actually is?

I think the RM wording seems a bit sloppy, but that "value" in this context refers to the things that are conceptually contained in the container (i.e. the length, and the elements at each index), and not to the representation.  I'd consider "capacity" to be part of the container's representation, not of its value.  Anyway, I think you're overthinking here (*).  The wording just means it's initialized to a container with no elements and length 0.

The assignment operator in Ada, however, isn't set up to copy "values" in a way that could change the representation, and it can't be overloaded the way assignment can in C++.  (This is actually a pretty cool feature of C++, as long as your program works perfectly the first time and never needs to be debugged or changed.)  What might be useful, that I don't see in the Bounded_Vectors spec, is a function that would return a vector with the same elements as an input parameter, but with a different capacity, e.g.

x : BC.Vector := BC.Copy_And_Change_Capacity (BC.Empty_Vector, Capacity => 42);

It wouldn't be all that useful for Empty_Vector since that's the default initialization anyway, but it conceivably could be useful in other cases.

(*) Yeah, I know, pot, kettle.

                                 -- Adam


  reply	other threads:[~2014-05-23  1:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-23  0:20 Empty Bounded Containers sbelmont700
2014-05-23  1:05 ` Adam Beneschan [this message]
2014-05-27 14:50   ` Adam Beneschan
2014-05-27 20:59     ` sbelmont700
2014-05-23  6:00 ` Simon Wright
2014-05-23 15:37   ` Adam Beneschan
2014-05-23 17:54     ` Simon Wright
2014-05-23 21:45 ` 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