Pure Contract bug?
Era Scarecrow
rtcvb32 at yahoo.com
Sat Feb 4 11:51:13 PST 2012
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.
More information about the Digitalmars-d-learn
mailing list