Bug 6942 – lazy parameters can break purity

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-13T04:48:00Z
Last change time
2011-12-02T18:56:15Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
timon.gehr

Comments

Comment #0 by timon.gehr — 2011-11-13T04:48:46Z
int foo(lazy int x) pure{ return x()+x(); } void main(){ auto a=foo((writeln("impure"),1)); } This compiles and prints impure impure
Comment #1 by timon.gehr — 2011-11-13T04:54:18Z
note that int foo(int delegate() x) pure{ return x()+x(); } void main(){ auto a=foo({return writeln("impure"),1;}); } fails with Error: pure function 'foo' cannot call impure delegate 'x'
Comment #2 by k.hara.pg — 2011-12-02T18:56:15Z
The pureness of lazy parameter belongs to the *caller side*, not callee side. It is a design. One use case is std.exception.enforce. It receives a condition as a lazy parameter, but whole enforce function can become pure with the design. Delegate parameter is similar to lazy parameter, but it is different in this point.