[Issue 6274] 'pure' for a whole struct definition

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Aug 12 12:44:07 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6274



--- Comment #1 from bearophile_hugs at eml.cc 2011-08-12 12:44:00 PDT ---
There is something I don't fully understand. The following code compiles with
the improvements in DMD 2.055alpha/head, but it needs a "pure" or before
"struct Map" (here#1) or at the front() method of Map (here#2). If both are
removed it doesn't compile. So is the pure attribute for struct partially
working already?



@property bool empty(T)(in T[] a) pure nothrow {
    return !a.length;
}

@property ref T front(T)(T[] a) pure nothrow {
    assert(a.length);
    return a[0];
}

void popFront(A)(ref A a) pure nothrow {
    assert(a.length);
    a = a[1 .. $];
}

pure struct Map(alias fun, R) { // here#1
    R _input;

    this(R input) nothrow pure {
        _input = input;
    }

    @property bool empty() nothrow const pure {
        return _input.empty;
    }

    @property auto ref front() nothrow const { // here#2
        return fun(_input.front);
    }

    void popFront() nothrow pure {
        _input.popFront();
    }
}

template map(alias fun) {
    auto map(R)(R range) {
        return Map!(fun, R)(range);
    }
}

int sqr(int x) pure nothrow { return x * x; }

pure nothrow int foo(int n) {
    int total;
    foreach (x; map!(sqr)([1, 2, 3, 4]))
        total += x;
    return total;
}

void main() {
    assert(foo(10) == 30);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list