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

           Summary: Various std.algorithm.map problems
           Product: D
           Version: unspecified
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nob...@puremagic.com
        ReportedBy: bearophile_h...@eml.cc


--- Comment #0 from bearophile_h...@eml.cc 2010-06-02 17:21:38 PDT ---
This D2 code works with DMD v2.046:

import std.stdio: writeln;
import std.algorithm: array, map;
void main() {
    int[] arr = [1, 2, 3];
    auto r1 = map!("a * a")(arr);
    writeln(array(r1));

    auto f = (int x){ return x * x; };
    auto r2 = map!(f)(arr);
    writeln(array(r2));
}

And prints:

1 4 9
1 4 9

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

But this program:

import std.stdio: writeln;
import std.algorithm: array, map;
void main() {
    int[] arr = [1, 2, 3];
    auto r3 = map!((x){ return x * x; })(arr);
    writeln(array(r3));
}


Generates the errors at compile-time:
...\src\phobos\std\algorithm.d(127): Error: forward reference to inferred
return type of function call __dgliteral1(0)
test.d(118): Error: template instance test.main.Map!(__dgliteral1,int[]) error
instantiating
test.d(5):        instantiated from here: map!(int[])
test.d(5): Error: template instance test.main.map!(__dgliteral1).map!(int[])
error instantiating

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

And this program:

import std.stdio: writeln;
import std.algorithm: array, map;
void main() {
    int[] arr = [1, 2, 3];
    auto r4 = map!((int x){ return x * x; })(arr);
    writeln(array(r4));
}


Generates the errors at compile-time:
test.d(5): Error: delegate std.algorithm.__dgliteral1 cannot access frame of
function __dgliteral1
test.d(5): Error: delegate std.algorithm.__dgliteral1 cannot access frame of
function __dgliteral1
test.d(5): Error: template instance test.main.map!(delegate int(int x)

{

return x * x;

}

) error instantiating

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

This D2 code works:

import std.stdio: writeln;
import std.algorithm: filter, array, map;
void main() {
    int[] arr = [1, 2, 3, 4, 5];
    auto r1 = array(filter!((x){ return x >= 3; })(arr));
    auto r2 = map!("a * a")(r1);
    writeln(array(r2));
}


And prints:

9 16 25

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

While this code:

import std.stdio: writeln;
import std.algorithm: filter, array, map;
void main() {
    int[] arr = [1, 2, 3, 4, 5];
    auto r1 = filter!((x){ return x >= 3; })(arr);
    auto r2 = map!("a * a")(r1);
    writeln(array(r2));
}


Generates the errors at compile-time:
...\src\phobos\std\algorithm.d(126): Error: struct
std.algorithm.Map!(result,Filter!(__dgliteral1,int[])).Map inner struct Filter
cannot be a field
...\src\phobos\std\algorithm.d(118): Error: template instance
std.algorithm.Map!(result,Filter!(__dgliteral1,int[])) error instantiating
test.d(6):        instantiated from here: map!(Filter!(__dgliteral1,int[]))
...\src\phobos\std\algorithm.d(6): Error: template instance
std.algorithm.map!("a * a").map!(Filter!(__dgliteral1,int[])) error
instantiating

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

This code:


import std.stdio: writeln;
import std.algorithm: array, map;
struct Range {
    int stop;
    int opApply(int delegate(ref int) dg) {
        int result;

        for (int i = 0; i < stop; i++) {
            result = dg(i);
            if (result)
                break;
        }

        return result;
    }
}
void main() {
    auto r = map!("a * a")(Range(6));
    writeln(array(r));
}



Generates the errors at compile-time:
...\src\phobos\std\algorithm.d(118): Error: template instance
Map!(result,Range) does not match template declaration Map(alias fun,Range) if
(isInputRange!(Range))
...\src\phobos\std\algorithm.d(118): Error: Map!(result,Range) is used as a
type
...\src\phobos\std\algorithm.d(120): Error: function expected before (), not
void of type void
...\src\phobos\std\algorithm.d(18): Error: template instance
std.algorithm.map!("a * a").map!(Range) error instantiating
test.d(18): Error: variable test.main.r voids have no value
test.d(18): Error: expression map((Range(6))) is void and has no value


A map() from a standard library must work with that Range() too because I have
structs/classes that define opApply and I want to use map and filter on them
too.

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

Reply via email to