A question about purity

bearophile bearophileHUGS at lycos.com
Mon Sep 12 15:24:05 PDT 2011


A D2 program:


struct Foo {
    int x;
    static int y;
    pure void foo1() {
        this.x++;
    }
    static pure void foo2() {
        Foo.y++; // line 8, error
    }
}
void main() {}


With DMD 2.055 it gives at compile-time:
test.d(8): Error: pure function 'foo2' cannot access mutable static data 'y'

If a (weakly?) pure method foo1 is allowed to change instance attributes, do you know why a static (weakly?) pure method can't change static attributes? What's the purity difference between foo1 and foo2? I cant' see a lot of difference.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list