DIP 1025--Dynamic Arrays Only Shrink, Never Grow--Community Review Round 1

Basile B. b2.temp at gmx.com
Mon Nov 11 21:49:52 UTC 2019


On Monday, 11 November 2019 at 10:27:26 UTC, Mike Parker wrote:
> This is the feedback thread for the first round of Community 
> Review for DIP 1025, "Dynamic Arrays Only Shrink, Never Grow":
>
> https://github.com/dlang/DIPs/blob/1b525ec4c914c06bc286c1a6dc93bf1533ee56e4/DIPs/DIP1025.md
>
> All review-related feedback on and discussion of the DIP should 
> occur in this thread. The review period will end at 11:59 PM ET 
> on November 25, or when I make a post declaring it complete.
>
> At the end of Round 1, if further review is deemed necessary, 
> the DIP will be scheduled for another round of Community 
> Review. Otherwise, it will be queued for the Final Review and 
> Formal Assessment.
>
> Anyone intending to post feedback in this thread is expected to 
> be familiar with the reviewer guidelines:
>
> https://github.com/dlang/DIPs/blob/master/docs/guidelines-reviewers.md
>
> *Please stay on topic!*
>
> Thanks in advance to all who participate.

As noted by the other reviewers,
- the restrictions proposed by the DIP imply much breaking 
changes.
- the problem is a lack of distinction between slices and dynamic 
arrays.

To that I also add that
- the restrictions would be disruptive for a certain usage of D, 
especially shell-like programs, manipulating array of files, 
array of directories, program arguments, etc.

so maybe that

1. At the local scope or when this is semantically possible, the 
compiler could keep track of the difference between a dynamic 
array and a slice, preventing growing.
2. When not possible anymore, e.g function parameter, a @slice 
attribute could be used to apply the same restriction as in 1.

---
void func1(int[] arr)
{
     arr ~= 1;               // OK
     arr.length += 1;        // OK
     auto slice = arr[0..2];
     slice.length += 1;      // NG, local restriction (1.)
}

void func2(@slice int[] arr)
{
     arr ~= 1;               // NG, @slice attribute (2.)
     arr.length += 1;        // NG, @slice attribute (2.)
     auto slice = arr[0..2];
     slice.length += 1;      // NG, local restriction (1.)
}
---

As an alternative, instead of 2. the new semantics could be 
applied only if the function is @safe (although this would give 
less control over individual parameters)

---
void func2(int[] arr)  // or @trusted
{
     arr ~= 1;               // OK, not @safe or @trusted
     arr.length += 1;        // OK, not @safe or @trusted
     auto slice = arr[0..2];
     slice.length += 1;      // NG, local restriction (1.)
}

void func3(int[] arr) @safe
{
     arr ~= 1;               // NG, not allowed in @safe func
     arr.length += 1;        // NG, not allowed in @safe func
     auto slice = arr[0..2];
     slice.length += 1;      // NG, local restriction (1.)
}
---

Of course ideally a full 1. would be better but then this 
requires complex DFA, which DMD doesn't know how to do yet, 
unless I'm not well informed.


More information about the Digitalmars-d mailing list