On Monday, 16 September 2013 at 07:01:52 UTC, Bienlein wrote:
Hello,
ich habe a pure method bar() in a class Foo:
class Foo {
int i = 0;
void bar() pure {
i++;
}
}
main() {
auto foo = new Foo();
foo.bar()
}
The pure method bar changes the inst var i. Nevertheless, the
code above compiles and runs which I find confusing. I assumed
that changing an inst var by a pure function is considered
creating a side efect. But the compiler has no problems with
this.
Am I getting something wrong here? Thanks for any hints.
Regards, Bienlein
(Weak) pure functions are allowed to mutate their arguments.
Methods take the object via a hidden parameter, so that's an
argument, too.
Mark all parameters const to get a strong pure function. For
"this" const goes on the method:
class Foo {
int i = 0;
void bar() const pure {
// can't mutate i here
}
}
See also: http://dlang.org/function.html#pure-functions