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

deadalnix deadalnix at gmail.com
Sat May 12 07:46:42 PDT 2012

Le 11/05/2012 22:29, Walter Bright a écrit :
> 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.

I did considered that quite a lot.

I came to the conclusion that function should have explicit and infered 

For simplicity, I will limit myself to pure here, the same can be 
extended to other attributes.

So, if a function is marked as pure, this function is EXPLICITELY pure. 
It means that the compiler ensure it is pure.

Otherwise, the function is marked as not explicitly pure, and purity is 
inferred unknown. When using such a function in a way that require a 
pure function, if nothing is explicitly stated, the inferred attribute 
is used. If the inferred attribute is unknown, the function body is used 
to infer it.

With such a mechanism, attribute are inferred when needed (otherwise, 
the calculation can become quite expansive).

If the function body is unknown (separate compilation model). Inference 
is impossible.

Alternatively, the DI generation could make inferred attributes explicit.

Such inference is very important for D. We have many qualifier, and it 
is unlikely that every programmer mark everything as it should. Just 
consider the state of druntime in that regard, and it have been done by 
experienced D programmers.

More information about the Digitalmars-d mailing list