Infer purity of functions too?

Mafi mafi at example.org
Thu Jul 21 06:52:45 PDT 2011


Am 21.07.2011 02:01, schrieb bearophile:
> 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

I would say the common type is int delegate(int) pure nothrow @trustd .


More information about the Digitalmars-d-learn mailing list