static array with inferred size
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Sep 20 01:29:39 UTC 2017
On Tuesday, September 19, 2017 20:47:25 Steven Schveighoffer via
Digitalmars-d wrote:
> This needs to happen.
>
> e.g.:
>
> char[$] arr = "hello"; // syntax up for debate, but I like this.
>
> I can't think of a correct way to do this that doesn't heap-allocate and
> is DRY.
>
> D is so powerful, it's a huge shame it can't figure this one out.
>
> issue: https://issues.dlang.org/show_bug.cgi?id=481
>
> Fix that was merged: https://github.com/dlang/dmd/pull/3615
>
> And then reverted: https://github.com/dlang/dmd/pull/4373
>
> Maybe there was an issue with the implementation? Can it be redone?
There have been previous attempts to implement this as a library solution.
IIRC, I was able to come up with a solution that would have been equivalent,
but I hit this issue with the compiler that prevented it:
https://issues.dlang.org/show_bug.cgi?id=16779
My solution would work so long as you gave up on VRP, and there are other
solutions which work in some cases but not all (e.g. requiring that all of
the elements of the array be known at compile time and thus disallowing
stuff like [42, a, 7, 9, b] where a and b are variables). I'm not aware of
any library solution which would infer the size that would have the full
functionality that you can get now when initializing a static array without
trying to infer the size.
All in all, I think that it would be cleanest if this were just implemented
in the language. I recall there being discussions about adding it with the
syntax you showed, and on reflection, I _think_ that I recall Walter
objecting to it for some reason, which is why it was reverted, but I don't
remember the details at all at this point, so I could easily be remembering
incorrectly.
Regardless, if we want the full functionality, we need improvements to the
compiler/language - be it implementing my enhancement request with regards
to VRP so that a library solution could work and/or by actually adding the
static array size inference feature to the language itself.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list