Comment #0 by bioinfornatics — 2012-10-23T05:27:40Z
Dear,
std.range should provide some pure function as;
iota -> 0 to to should give evey time the same result
zip -> when it is an array (not an associative array) should be always the same result
and other …
this missing feature allowed developers to use the pure in many case
Comment #1 by issues.dlang — 2012-10-23T11:22:28Z
pure is inferred for templated functions. As long as the functions for the range used are pure, then the functions in std.range and std.algorithm will generally be pure. There may be some compiler bugs which make it so that that doesn't always work like it's supposed to, but pretty much zero functions in std.range and std.algorithm should be marked as pure, since whether they can be pure or not really depends on the types used with them, which is why attribute inferrence for templates was introduced in the first place.
If you have an issue with a specific function with a specific type which does have pure member functions and the templated function is not being inferred as pure, then please report it with an appropriate code sample, but it would be wrong to specifically provide pure versions of functions like iota or zip.
Comment #2 by bioinfornatics — 2012-10-23T12:13:51Z
In first thanks jonathan to your answer.
Code below fail both when using pure with dmd/ldc dmdfe 2.060 => http://dpaste.dzfl.pl/65efd36e
___________________________________________
import std.range;
import std.stdio;
pure uint square( in int[] x, in int[] y ){
uint result = 0;
foreach( item; zip( x, y ) )
result += item[0] * item[1];
return result;
}
int main(){
int[3] a = [0,1,2];
int[3] b = [1,2,3];
writefln( "Square of %s with %s give %u", a, b, square( a, b ) );
}
Comment #3 by bearophile_hugs — 2012-10-23T14:13:32Z
(In reply to comment #1)
> If you have an issue with a specific function with a specific type which does
> have pure member functions and the templated function is not being inferred as
> pure, then please report it with an appropriate code sample, but it would be
> wrong to specifically provide pure versions of functions like iota or zip.
This gives:
test.d(3): Error: pure function 'main' cannot call impure function 'iota'
import std.range: iota;
void main() pure {
iota(10);
}
This gives:
test.d(4): Error: pure function 'main' cannot call impure function 'map'
test.d(4): Error: map is not nothrow
test.d(2): Error: function D main 'main' is nothrow yet may throw
import std.algorithm: map;
void main() pure nothrow {
int[] data = [1, 2, 3];
auto r = map!q{a * a}(data);
}
I propose to reopen this bug report, and change its meaning and title: instead of asking for such functions to be marked as pure, to ask them to be usable in a pure (and sometimes nothrow) situations.
Comment #4 by issues.dlang — 2012-10-23T15:25:37Z
It would probably be cleaner to just create a new bug report, but you can reopen this one if you want to. Regardless, the bug as initially reported is incorrect.
Comment #5 by bearophile_hugs — 2012-10-23T16:44:32Z
(In reply to comment #4)
> It would probably be cleaner to just create a new bug report,
Thank you for the answer. I have opened Issue 8882