www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13981] New: std.algorithm: inconsistent handling of static

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

          Issue ID: 13981
           Summary: std.algorithm: inconsistent handling of static arrays
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: markus oberhumer.com

I'd guess this is a typical D newcomer issue as static arrays
  - are passed by value
  - are allocated on the stack (unless global)
  - are not real ranges because they lack popFront()

Unfortunately I had to dig through various bugreports at issues.dlang.org
before better understanding this issue, so I'd suggest adding a short comment
to algorithms.d.

In any case it's confusing that reduce() seems to work for static arrays while
sum() does not.


// dmd 2.066.1
import std.algorithm;

void test_dynamic_array() {
    int[] arr1 = [1, 2, 3, 4, 5];
    immutable int[] arr2 = [1, 2, 3, 4, 5];

    filter!"a & 1"(arr1);       // OK
    map!"a + 10"(arr1);         // OK
    reduce!"a + b"(arr1);       // OK
    sum(arr1);                  // OK

    filter!"a & 1"(arr2);       // OK
    map!"a + 10"(arr2);         // OK
    reduce!"a + b"(arr2);       // OK
    sum(arr2);                  // OK
}

void test_static_array() {
    int[5] arr1 = [1, 2, 3, 4, 5];
    static immutable int[5] arr2 = [1, 2, 3, 4, 5];

    //filter!"a & 1"(&arr1);      // Error: won't compile
    //map!"a + 10"(arr1);         // Error: won't compile
    reduce!"a + b"(arr1);       // OK !
    //sum(arr1);                  // Error: won't compile

    //filter!"a & 1"(arr2);       // Error: won't compile
    //map!"a + 10"(arr2);         // Error: won't compile
    //reduce!"a + b"(arr2);       // Error: won't compile ???
    //sum(arr2);                  // Error: won't compile
}

--
Jan 14 2015