D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6942 - lazy parameters can break purity
Summary: lazy parameters can break purity
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2011-11-13 04:48 UTC by timon.gehr
Modified: 2011-12-02 18:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description timon.gehr 2011-11-13 04:48:46 UTC
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 timon.gehr 2011-11-13 04:54:18 UTC
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 Kenji Hara 2011-12-02 18:56:15 UTC
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.