Pure Contract bug?
Timon Gehr
timon.gehr at gmx.ch
Sat Feb 4 12:45:12 PST 2012
On 02/04/2012 08:51 PM, Era Scarecrow wrote:
> If I'm reading how pure works, my original example was likely broken as
> it was part of a struct that returned a state value (although the
> contract constraints meaning was still valid).
>
> So is pure fully usable or is it not yet ready? Makes me think that pure
> should have further constraints/limits, if it's part of a class/struct
> it should either require or automatically be static (no state access)
> and if it accesses any global variables, they must be immutable.
>
> int x;
> immutable int y = 10;
>
> pure int test(int z) {
> int t = z + x; //should error
> t += y; //fine, globally immutable.
> return t;
> }
>
> struct X {
> int s_x;
> static int s_st_x;
> immutable int s_y;
> static immutable int s_st_y = 100;
>
> pure int test(int z) {
> int t = x + z; //should error
> t += s_x; //error, mutable external state
> t += s_st_x; //error, mutable external state (static)
> t += s_y; //error, not statically immutable, mathematically impure.
>
>
> t += y; //fine, global immutable
> t += s_st_y; //fine, statically immutable.
> return t;
> }
> }
>
> Errors I get currently are these:
>
> test(int):
> Error: pure function 'test' cannot access mutable static data 'x'
>
> X.test(int):
> Error: pure function 'test' cannot access mutable static data 'x'
> Error: pure function 'test' cannot access mutable static data 's_st_x'
>
>
> If I understand pure correctly, I should get two more, for s_x and s_y.
Pure does not imply const in D. If you want stronger guarantees, just
turn 'test' into a const (or immutable) member function. In D, a
function can change anything that is mutable and reachable through its
parameters, this includes the implicit 'this' pointer. The reason for
this design is simple: There is no need to be overly restrictive,
because the additional restrictions are already trivially expressed in
the type system. Furthermore, any mutation of a parameter can be turned
into a less efficient protocol that only requires a const pure function,
so there would be no gain if pure implied const, it would only make pure
less useful.
void foo(int* x)pure{*x+=2;}
int bar(const(int)*x)pure{return *x+2;}
void main() pure{
int x, y;
foo(&x); // those two lines have
y = bar(&y); // equivalent functionality!
}
More information about the Digitalmars-d-learn
mailing list