[Issue 7444] New: Require [] for array copies too

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 5 11:10:59 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7444

           Summary: Require [] for array copies too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2012-02-05 11:10:57 PST ---
This is derived from issue 3971 see there for more (messy) discussions.

In 2.058 this compiles:


int[100] foo() {
    int[100] a;
    return a;
}
void main() {
    int[10_000] a, b;
    auto c = new int[10_000];
    a = 1;
    a = b;
    a = c;
    auto d = foo();
}


But for consistency of the vector ops syntax, and to help the programmer spot
where the code is doing a potentially long copy, I suggest to turn that first
program into a compile-time error, and require [] on lvalues everywhere you
perform a vector operation, like when you copy a int[N] on another int[N], etc:


int[100] foo() {
    int[100] a;
    return a;
}
void main() {
    int[10_000] a, b;
    auto c = new int[10_000];
    a[] = 1;
    a[] = b;
    a[] = c;
    auto d[] = foo(); // currently an error
}

- - - - - - - - - - - - - - - -

But note that normally all array ops require a [] even on the rvalue:


void main() {
    int[10_000] a, b;
    a[] += b[];
}



So an alternative stricter proposal is to require [] on the right too:


int[100] foo() {
    int[100] a;
    return a;
}
void main() {
    int[10_000] a, b;
    auto c = new int[10_000];
    auto d = new int[10_000];
    a[] = 1;
    a[] = b[];
    a[] = c[];
    c[] = d[];
    auto e[] = foo()[]; // currently an error
}



It helps the programmer tell apart two different cases, that currently are
usable with the same syntax (from an example by Don):


void main() {
    int[][3] x;
    int[]    y;
    int[]    z;

    x[] = z; // copies just the z pointer
    y[] = z; // copies the elements in z
}



Requiring [] on the right (rvalue) for the copy of many items avoids the
ambiguity:

void main() {
    int[][3] x;
    int[]    y;
    int[]    z;
    x[] = z;
    y[] = z[];
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list