Jump to page: 1 24  
Page
Thread overview
static array with inferred size
Sep 20, 2017
Jonathan M Davis
Sep 20, 2017
Meta
Sep 20, 2017
jmh530
Sep 20, 2017
jmh530
Sep 21, 2017
jmh530
Sep 20, 2017
Jonathan M Davis
Sep 20, 2017
Dgame
Sep 20, 2017
Jonathan M Davis
Sep 20, 2017
Nordlöw
Sep 20, 2017
Dgame
Sep 20, 2017
Nordlöw
Sep 20, 2017
Jonathan M Davis
Sep 20, 2017
Per Nordlöw
Sep 20, 2017
Jonathan M Davis
Sep 20, 2017
Stefan Koch
Sep 20, 2017
Meta
Sep 20, 2017
Jonathan M Davis
Sep 20, 2017
Timon Gehr
Sep 21, 2017
Per Nordlöw
Sep 21, 2017
Timon Gehr
Sep 22, 2017
Jonathan M Davis
Sep 20, 2017
Dgame
Sep 20, 2017
ag0aep6g
Sep 20, 2017
Ilya
September 19, 2017
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?

-Steve
September 19, 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

September 20, 2017
On Wednesday, 20 September 2017 at 01:29:39 UTC, Jonathan M Davis wrote:
> 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?
>
> 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.

That was the main reason it was reverted. A contributing factor is that Beadophile had been trying to push this feature for a long time, and once it got in (against W&A's reservations, although they eventually gave the okay) he started pushing harder for the []s syntax for static array literals, arguing that now that we had static array length deduction syntax we needed static array literal syntax as well. This was the straw that broke the camel's back for Andrei and he decided to revert the length deduction PR citing concerns over feature bloat. There was also other functionality tied up with the deduction syntax - see this post: https://forum.dlang.org/post/wagryggxehnbsbyhwkgf@forum.dlang.org

With all due respect to Andrei, I think he overreacted a bit and it was a mistake to revert static array length deduction (although the array/aa type deduction on steroids was probably overly complicated so that was a good call). Maybe now that @nogc and betterC are squarely in focus we can revisit array length deduction.
September 20, 2017
On Wednesday, 20 September 2017 at 02:46:53 UTC, Meta wrote:
>
> [snip]

I also favor making arr[..] equivalent to arr[0..$] and allowing overloading of \ (for inverse, similar syntax as Matlab).
September 19, 2017
On 9/19/17 10:46 PM, Meta wrote:
> With all due respect to Andrei, I think he overreacted a bit and it was a mistake to revert static array length deduction (although the array/aa type deduction on steroids was probably overly complicated so that was a good call). Maybe now that @nogc and betterC are squarely in focus we can revisit array length deduction.

The length deduction is so obviously needed, especially when you want to avoid heap allocations.

char["hello".length] = "hello";

It's just so terrible. I can't figure out a good way around it.

Deducing types is probably reasonable as a request, but I don't see how one requires the other. There is no need to repeat the entire literal to form the type, and you aren't specifying the type anyway. At most you have to enter the type one more time (in the case of explictly typing an element).

But having to manually count all the elements in an array literal? That is torture! D should be better than that.

-Steve
September 20, 2017
On 9/19/17 8:47 PM, Steven Schveighoffer 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?
> 
> -Steve

The argument was it can be done trivially with a library solution. -- Andrei
September 20, 2017
On Wednesday, 20 September 2017 at 00:47:25 UTC, Steven Schveighoffer wrote:
> This needs to happen.
>
> e.g.:
>
> char[$] arr = "hello"; // syntax up for debate, but I like this.
>

Yes, hope to see this one day as a language feature, not a library solution. --Ilya
September 20, 2017
On Wednesday, September 20, 2017 01:36:43 Andrei Alexandrescu via Digitalmars-d wrote:
> On 9/19/17 8:47 PM, Steven Schveighoffer 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?
> >
> > -Steve
>
> The argument was it can be done trivially with a library solution. -- Andrei

I have yet to see a library solution that is able to accept the full range of arguments that you can give when you provide the length. The closest that I was able to come up with didn't work with VRP, and most of the other solutions I've seen only accept compile-time arguments, meaning that you can't having any variables in the elements used to initialize the static array. I opened

https://issues.dlang.org/show_bug.cgi?id=16779

a while ago, since with that enhancement implemented, it _is_ possible to do the same thing that T[$] = would do, but without that, I don't know of any way to get the full functionality that we get right now when the size isn't inferred.

Now, I think that something like T[$] = is a very improvement syntactically, so I'd be in favor of it anyway, but regardless, as far as I can tell, if we want this full functionality, we need to improve to the language/compiler - be it by adding syntax for infering the size to the language itself or by fixing it so that VRP works properly with IFTI and auto ref.

- Jonathan M Davis

September 20, 2017
On Wednesday, 20 September 2017 at 05:36:43 UTC, Andrei Alexandrescu wrote:
> On 9/19/17 8:47 PM, Steven Schveighoffer 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?
>> 
>> -Steve
>
> The argument was it can be done trivially with a library solution. -- Andrei

http://p0nce.github.io/d-idioms/#@nogc-Array-Literals:-Breaking-the-Limits
September 20, 2017
On Wednesday, September 20, 2017 07:12:15 Dgame via Digitalmars-d wrote:
> On Wednesday, 20 September 2017 at 05:36:43 UTC, Andrei
>
> Alexandrescu wrote:
> > On 9/19/17 8:47 PM, Steven Schveighoffer 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?
> >>
> >> -Steve
> >
> > The argument was it can be done trivially with a library solution. -- Andrei
>
> http://p0nce.github.io/d-idioms/#@nogc-Array-Literals:-Breaking-the-Limits

Yeah, it's a partial solution, but it won't work with VRP. e.g.

ubyte[4] = [1, 2, 3, 4];

is legal, but that won't work with that template.

https://issues.dlang.org/show_bug.cgi?id=16779

Also, that example

T[n] s(T, size_t n)(auto ref T[n] array) pure nothrow @nogc @safe
{
    return array;
}

void main() @nogc
{
    int[] myDynamicArray = [1, 2, 3].s; // Slice that static array which is
on stack

    // Use myDynamicArray...
}

looks like it shouldn't even be legal, since the static array should be gone as soon as the line where myDynamicArray is declared has passed. Using auto should be fine, because then it would be a static array, and the data would be copied to the stack, but assigning it to a dynamic array should be illegal.

- Jonathan M Davis

« First   ‹ Prev
1 2 3 4