Thread overview
[Issue 4665] New: map with no automatic closure
Aug 30, 2010
Andrej Mitrovic
Jul 27, 2012
Brad Anderson
August 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4665

           Summary: map with no automatic closure
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-08-17 07:22:23 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4665


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-29 18:18:53 PDT ---
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"]);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4665


Brad Anderson <eco@gnuk.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eco@gnuk.net


--- Comment #2 from Brad Anderson <eco@gnuk.net> 2012-07-27 00:34:03 PDT ---
The code works in DMD 2.060 beta.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4665


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #3 from bearophile_hugs@eml.cc 2012-07-27 04:53:38 PDT ---
(In reply to comment #2)
> The code works in DMD 2.060 beta.

Right. Closed.

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