Array types not treated uniformly when passed as ranges

Jonathan M Davis jmdavisProg at gmx.com
Tue Feb 15 13:58:19 PST 2011


On Tuesday, February 15, 2011 13:15:22 spir wrote:
> On 02/15/2011 03:10 PM, Steven Schveighoffer wrote:
> > On Mon, 14 Feb 2011 22:59:52 -0500, Jonathan M Davis <jmdavisProg at gmx.com> 
wrote:
> >> It's because an Array is not a range. Dynamic arrays are a bit special
> >> in that they're both a container and a range. An Array is just a
> >> container. But honestly, you wouldn't really want it to work.
> > 
> > Dynamic arrays are not containers. They do not own the data they
> > reference, they just reference that data. In fact, the owner of the data
> > is really the runtime.
> > 
> > The naming of Array makes this a difficult thing to understand. An Array
> > owns its data, it manages the data, creates it, destroys it, and there
> > is no way to "slice" the Array into a smaller Array. It's a true
> > reference type. A builtin array is not really a container, so it really
> > should be named differently. But there's no way to change that.
> > 
> > [snip]
> > 
> >> You don't have the problem with arrays that you'd have with user-defined
> >> container types, because the semantics of arrays are a bit odd when you
> >> copy them. So, if anything were faulty, it would be the built in
> >> arrays, not user- defined container types like Array. It is quite handy
> >> to have arrays work how they work, however, so that's not likely to
> >> change.
> > 
> > What is faulty is that they are called arrays. They are slices.
> 
> It took me some time to really understand builtin dynamic arrays, but since
> then have had no issue with them. Instead, I now appreciate their "bastard"
> semantics/behaviour ;-) But I agree they're weird, hard to explain, and
> their relation to ranges maybe still lacks integration.
> 
> How would speak about static arrays, from this point of view? Do you also
> consider them rather slices (view upon external data?) than containers in
> the strict sense of the term?

No, they _are_ containers. They're value types. If they go away, their memory 
goes away. They _do_ own their data. That's why you have to slice them (and 
therefore get a dynamic array to them) when you pass them to range functions.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list