pure-ifying my code
Jonathan M Davis
jmdavisProg at gmx.com
Sun Nov 17 02:17:16 PST 2013
On Sunday, November 17, 2013 10:05:30 Philippe Sigaud wrote:
> I'm trying to put a bit of `pure` and `const`/`immutable` in my
> code, and I'm getting strange error messages.
>
> Namely, I've a pure function, says `foo` that returns
> `std.algorithm.joiner(someValue)`:
>
> import std.algorithm: joiner;
> auto foo() pure
> {
> // some internal calculation
> // creating `someValue`
> return joiner(someValue);
> }
>
> DMD tells me `foo` cannot use the impure `joiner`, due to
> `joiner`'s internal struct (Result) not having pure methods
> (`empty`/`front`/`popFront`).
>
> Now, it seems obvious why these range methods are not pure
> (`popFront`, at least).
All of them should be able to be pure, as none of them access global or static
variables.
> But I don't use them in my function! I
> just return a lazy range to iterate on other ranges. My own
> function do not mutate anything, it just creates an object. A
> mutable value admittedly, but not one with global state.
>
> I know Phobos is not using `pure` as much as it could right now,
> but I really don't see why it's causing trouble right there.
Likely because it's calling an impure constructor. The compiler does not
currently properly infer purity for Voldemort types.
https://d.puremagic.com/issues/show_bug.cgi?id=10329
Really, the compiler's attribute inferrence is pretty pathetic at this point.
It only manages to infer attributes in the most basic of cases, and that's the
number one reason that so little of Phobos works with pure right now.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list