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

           Summary: Immutable function pointer arguments too
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nob...@puremagic.com
        ReportedBy: bearophile_h...@eml.cc


--- Comment #0 from bearophile_h...@eml.cc 2012-02-20 14:51:48 PST ---
This problem may be correlated to Issue 7500

This code doesn't compile because 'foo' is mutable:


T outer(T)(T function(in T) pure foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}


test.d(3): Error: pure nested function 'inner' cannot access mutable data 'foo'
test.d(11): Error: template instance test.outer!(int) error instantiating


But it doesn't work if you add an 'immutable':


T outer(T)(immutable T function(in T) pure foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}


test.d(11): Error: template test.outer(T) cannot deduce template function from
argument types !()(int function(const(int) x) pure)



This compiles, but it's not nice:


int sqr(in int x) pure {
     return x * x;
}
immutable sqrPtr =&sqr;
auto outer(typeof(sqrPtr) foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
void main() {
     assert(outer(sqrPtr) == 25);
}



A better workaround, found by Timon Gehr:

T outer(T)(T function(in T) pure foo) pure {
     immutable fooTick = foo;
     pure int inner() {
         return fooTick(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}

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

Reply via email to