Bug 4665 – map with no automatic closure

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-08-17T07:22:00Z
Last change time
2012-07-27T04:53:38Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-08-17T07:22:23Z
This D2 program seems correct: import std.algorithm: map; void main() { int n = 2; map!((double x){ return x * n; })([1.0, 2.0]); } But DMD 2.048 prints at run time: object.Error: Access Violation On IRC #D jA_cOp has commented that TDPL says that the compiler should automatically choose delegate over function if it accesses its outer lexical scope.
Comment #1 by andrej.mitrovich — 2010-08-29T18:18:53Z
It does seem to be a delegate after all. If you try to evaluate it right there it shows (notice the parantheses after the lambda definition): import std.algorithm: map; void main() { int n = 2; map!( (double x){ return x * n; }() )([1.0, 2.0]); } Errors: test.d(10): Error: expected 1 function arguments, not 0 test.d(10): Error: cannot evaluate delegate double(double x) { return x * cast(double)n; } () at compile time This seems like a compiler bug. If you use a string instead, then it will work: import std.algorithm: map; void main() { string n = "2"; map!( (string x){ return x ~ n; } )(["1.0", "2.0"]); }
Comment #2 by eco — 2012-07-27T00:34:03Z
The code works in DMD 2.060 beta.
Comment #3 by bearophile_hugs — 2012-07-27T04:53:38Z
(In reply to comment #2) > The code works in DMD 2.060 beta. Right. Closed.