Allocating a slice object
Jonathan M Davis
jmdavisProg at gmx.com
Thu Jul 4 09:40:27 PDT 2013
On Thursday, July 04, 2013 09:37:57 Steven Schveighoffer wrote:
> On Thu, 04 Jul 2013 08:02:13 -0400, monarch_dodra <monarchdodra at gmail.com>
>
> wrote:
> > This is a pretty stupid question, but how would you allocate an "int[]"
> > on the heap? I'm not talking about the array, but the actual slice
> > object. EG:
> >
> > int[]* pSlice = new int[];
> > //Error: new can only create structs,
> > //dynamic arrays or class objects, not int[]'s
> >
> > Is there a simple "idiomatic" way?
>
> Not really. There was talk at one point of deprecating that version, so
> you had to do new int[](5) instead of new int[5], and then using new
> int[5] to mean new "fixed sized array of size 5 on the heap", and then new
> int[] would mean new slice on the heap.
>
> But I think there's a real lack of benefit to this, plus it would be
> confusing to people familiar with other languages.
If we were to make such a change, I'd argue for making int[5] outright illegal
simply because it _would_ be too confusing for folks from other languages, but
I really do wish that what we had with arrays was cleaner in this respect. We
kind of get away with new int[5] and new int[](5) being the same thing, but
then everyone is thrown off once you go to another level - e.g. new int[5][2].
So, I think that D's apporoach to this was a definite mistake. But also going
full-out and making it so that every time the number is between the brackets,
it's a static array, and every time it's between the parens it's a dynamic
array isn't exactly pretty either - particularly given the confusion around
the common use case. So, I don't know quite what we should have done. It
almost makes me think that static arrays should have outright not used
brackets.
In any case, we're pretty much stuck with what we have at this point,
regardless of what the ideal solution would have been.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list