Why isn't purity & co. inferred for all functions?

Walter Bright newshound2 at digitalmars.com
Fri May 11 13:29:25 PDT 2012


On 5/11/2012 11:53 AM, Trass3r wrote:
> It's already inferred for templated functions, so it's perfectly possible.
> Also it should be checked anyway, so I get an error message if I marked
> something pure or whatever while it actually isn't.
>
> The only functions where it can't be inferred and manual attributes are really
> needed are function definitions (i.e. no body available).
>
> What speaks against it?

Separate compilation.

I.e. given modules A and B:

A
===
int foo() { return 3; }
===

B
===
import A;

pure int bar() { return foo(); }
===

and the compiler infers purity, so it compiles.

Then, maintenance programmer Fred updates module A to be:

A
===
int x;
int foo() { return x; }
===

and now foo() is no longer pure. He recompiles A, links with existing B.obj, and 
now whoops! bar() is marked as pure, but is not pure.

This issue does not exist for templates & literals & auto funcs, because already 
A must be recompiled if B is modified.


More information about the Digitalmars-d mailing list