wilzbach / tools-test

1 stars 0 forks source link

std.range is not pure #89

Closed wilzbach closed 9 years ago

wilzbach commented 11 years ago

Note: the issue was created automatically migrated from https://issues.dlang.org

Original bug ID: BZ#8878 From: bioinfornatics <bioinfornatics@gmail.com> Reported version: D2 CC: bearophile_hugs@eml.cc, issues.dlang@jmdavisProg.com

wilzbach commented 11 years ago

Comment author: bioinfornatics <bioinfornatics@gmail.com>

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

wilzbach commented 11 years ago

Comment author: Jonathan M Davis <issues.dlang@jmdavisProg.com>

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.

wilzbach commented 11 years ago

Comment author: bioinfornatics <bioinfornatics@gmail.com>

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 ) ); }

wilzbach commented 11 years ago

Comment author: bearophile_hugs@eml.cc

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.

wilzbach commented 11 years ago

Comment author: Jonathan M Davis <issues.dlang@jmdavisProg.com>

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.

wilzbach commented 11 years ago

Comment author: bearophile_hugs@eml.cc

It would probably be cleaner to just create a new bug report,

Thank you for the answer. I have opened Issue 8882