Bug 11412 – Allow nested pure functions to access outer function variables

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-01T09:57:00Z
Last change time
2015-03-25T05:19:51Z
Assigned to
nobody
Creator
verylonglogin.reg

Comments

Comment #0 by verylonglogin.reg — 2013-11-01T09:57:39Z
As an outer function is a context for its nested function it should be accessible for weakly pure functions just like type instance for member functions: --- struct S { int i; void f() pure { int j; void g() pure { ++i; // ok ++j; // currently error, looks inconsistent } } } --- Also this will allow things like this (usable e.g. for std.conv): --- struct S { void toString(scope void delegate(const(char)[]) pure) pure; } string sToStr(S s) pure { string res; s.toString((chars) { res ~= chars; }); return res; } ---
Comment #1 by verylonglogin.reg — 2013-11-14T03:34:57Z
This enhancement is important as currently lots of `std.algorithm`/`std.range` stuff can't used in `pure` functions because of closures: --- import std.algorithm, std.range; size_t notXCount(in int[] arr, int x) pure { return arr .filter!(n => n != x)() .walkLength(); // currently error as predicate is impure } ---
Comment #2 by k.hara.pg — 2015-03-25T05:19:51Z
Will be fixed from 2.067. *** This issue has been marked as a duplicate of issue 9148 ***