Infer purity of functions too?
bearophile
bearophileHUGS at lycos.com
Wed Jul 20 17:01:28 PDT 2011
Jonathan M Davis:
> I believe that there is some level of purity inference with delegates, but I
> don't know what the situation with that is exactly.
I didn't know/remember this, but it seems you are right, delegates too seem to receive purity inference. This has one downside too, this innocent-looking program doesn't compile:
import std.math;
void main() {
auto funcs = [(int x){return x; },
(int x){return abs(x); }];
}
The error, DMD 2.054:
test.d(4): Error: incompatible types for ((__dgliteral1) ? (__dgliteral2)): 'int delegate(int x) pure nothrow' and 'int delegate(int x) nothrow'
How to solve this specific problem: I think abs() can be pure.
An example of a bit more general class of problems:
import std.math;
void main() {
auto funcs = [&sin, &tan];
}
test.d(3): Error: incompatible types for ((& sin) ? (& tan)): 'real function(real x) pure nothrow @safe' and 'real function(real x) pure nothrow @trusted'
To solve this more generic class of problems the compiler has to give to the funcs array a common type (where possible), here int delegate(int x) pure nothrow[] is able to contain both kinds of delegates, and cast all the array delegates to the common type (I have a bug report about this in Bugzilla).
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list