Currently, a function is considered to be strongly pure if all its formal parameters implicitly convert to immutable.
But a function invocation can be considered strongly pure if all actual arguments are implicitly convertible to immutable.
The following code should compile:
const(int)[] foo(const(int)[] x)pure{return x;}
void main(){
immutable(int)[] x;
immutable(int)[] y = foo(x);
}
Comment #1 by bearophile_hugs — 2012-01-18T14:49:09Z
I think there are ways to further improve this idea. And I think such ideas will lead to quite useful code. I have recently written two posts about related ideas. I think this is an improvement.
But I think language complexity too should be kept into account. When a programmer writes code like this:
auto foo(...) pure { ... }
void main() {
immutable x = foo(...);
}
She should be able to usually guess correctly if such code is correct, or if it doesn't compile. The more complex D purity becomes, the less easy is to remember all the rules and exceptions to the rule, and this increases the risk of turning programming more into guesswork than design. This sometimes happens even if every single rule looks like an improvement.
Comment #2 by maxhaton — 2021-01-24T06:46:16Z
spec problem + this behaviour would make it inconsistent with regular assignments
Comment #3 by timon.gehr — 2021-01-24T12:31:55Z
???
Comment #4 by robert.schadek — 2024-12-13T17:57:42Z