comp.lang.ada
 help / color / mirror / Atom feed
* Empty Bounded Containers
@ 2014-05-23  0:20 sbelmont700
  2014-05-23  1:05 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: sbelmont700 @ 2014-05-23  0:20 UTC (permalink / raw)



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.  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?

-sb


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

* Re: Empty Bounded Containers
  2014-05-23  0:20 Empty Bounded Containers sbelmont700
@ 2014-05-23  1:05 ` Adam Beneschan
  2014-05-27 14:50   ` Adam Beneschan
  2014-05-23  6:00 ` Simon Wright
  2014-05-23 21:45 ` Randy Brukardt
  2 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2014-05-23  1:05 UTC (permalink / raw)


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


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

* Re: Empty Bounded Containers
  2014-05-23  0:20 Empty Bounded Containers sbelmont700
  2014-05-23  1:05 ` Adam Beneschan
@ 2014-05-23  6:00 ` Simon Wright
  2014-05-23 15:37   ` Adam Beneschan
  2014-05-23 21:45 ` Randy Brukardt
  2 siblings, 1 reply; 8+ messages in thread
From: Simon Wright @ 2014-05-23  6:00 UTC (permalink / raw)


sbelmont700@gmail.com writes:

> 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.  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.

It is initialized to the same value in the sense that your second 'x'
above "=" BC.Empty_Vector.

(Am I the only one left who thinks of the Ada 95 Booch Components on
seeing 'BC'? some initial confusion here!)


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

* Re: Empty Bounded Containers
  2014-05-23  6:00 ` Simon Wright
@ 2014-05-23 15:37   ` Adam Beneschan
  2014-05-23 17:54     ` Simon Wright
  0 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2014-05-23 15:37 UTC (permalink / raw)


On Thursday, May 22, 2014 11:00:06 PM UTC-7, Simon Wright wrote:

> (Am I the only one left who thinks of the Ada 95 Booch Components on
> seeing 'BC'? some initial confusion here!)

No, you're not.  The same thought occurred to me when I was reading it.  Hope that makes you feel better.

                                  -- Adam

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

* Re: Empty Bounded Containers
  2014-05-23 15:37   ` Adam Beneschan
@ 2014-05-23 17:54     ` Simon Wright
  0 siblings, 0 replies; 8+ messages in thread
From: Simon Wright @ 2014-05-23 17:54 UTC (permalink / raw)


Adam Beneschan <adambeneschan@gmail.com> writes:

> On Thursday, May 22, 2014 11:00:06 PM UTC-7, Simon Wright wrote:
>
>> (Am I the only one left who thinks of the Ada 95 Booch Components on
>> seeing 'BC'? some initial confusion here!)
>
> No, you're not.  The same thought occurred to me when I was reading
> it.  Hope that makes you feel better.

Well, I'm in good company there then! I guess I have more reason than
most, though.

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

* Re: Empty Bounded Containers
  2014-05-23  0:20 Empty Bounded Containers sbelmont700
  2014-05-23  1:05 ` Adam Beneschan
  2014-05-23  6:00 ` Simon Wright
@ 2014-05-23 21:45 ` Randy Brukardt
  2 siblings, 0 replies; 8+ messages in thread
From: Randy Brukardt @ 2014-05-23 21:45 UTC (permalink / raw)


<sbelmont700@gmail.com> wrote in message 
news:cfe5082c-9b50-4c5b-afe1-8dd6ea1640b9@googlegroups.com...
...
>which is not intuitively what I would assume.  For instance, the following 
>line:
>
>x : BC.Vector (Capacity => 42) := BC.Empty_Vector;

The problem here is using ":=" on bounded containers. Because of the 
capacity discriminant, it will never work quite right.

The whole reason that Assign and Copy were introduced into the Ada 2012 
containers is to work around this problem.

You want:
  X : BC.Vector (Capacity => 42) := Copy(BC.Empty_Vector, Capacity => 42);

Or:

   X : BC.Vector (Capacity => 42);

   BC.Assign (Target => X, Source => BC.Empty_Vector);

Ada has no way to avoid discriminant checks in ":=", so there is no 
possibility of making it work like you would want.

                                  Randy.


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

* Re: Empty Bounded Containers
  2014-05-23  1:05 ` Adam Beneschan
@ 2014-05-27 14:50   ` Adam Beneschan
  2014-05-27 20:59     ` sbelmont700
  0 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2014-05-27 14:50 UTC (permalink / raw)


On Thursday, May 22, 2014 6:05:19 PM UTC-7, Adam Beneschan wrote:

I wrote:

> 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);

As Randy pointed out, there *is* already a function like that, Copy, that was added in Ada 2012.  I don't know how I missed it.  I looked through the RM chapter on Containers.Vectors several times looking for this, and I still couldn't find it.  Time to stop putting off that appointment with my ophthalmologist, I guess.

Sigh ...

                                -- Adam

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

* Re: Empty Bounded Containers
  2014-05-27 14:50   ` Adam Beneschan
@ 2014-05-27 20:59     ` sbelmont700
  0 siblings, 0 replies; 8+ messages in thread
From: sbelmont700 @ 2014-05-27 20:59 UTC (permalink / raw)


On Tuesday, May 27, 2014 10:50:04 AM UTC-4, Adam Beneschan wrote:
>
> As Randy pointed out, there *is* already a function like that, Copy, that was added in Ada 2012.  
> 


Ah, I had missed that as well.  It was mostly just out of habit (as well as often forgetting the syntax) that I like to explicitly initialize every element of an aggregate, and it didn't occur to me that "copying" an "empty vector" would be equivalent.

Thank you to everyone for your responses.

-sb


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

end of thread, other threads:[~2014-05-27 20:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-23  0:20 Empty Bounded Containers sbelmont700
2014-05-23  1:05 ` Adam Beneschan
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

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