www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5746] New: Make std.range.iota strongly pure

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

           Summary: Make std.range.iota strongly pure
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



Generally I'd like Phobos functions/ranges to be pure unless their semantics is
clearly not pure. I'd like to use iota() in pure functions too. In the
semantics of iota() there is fundamentally nothing that prevents it to be pure.

This D2 program:


import std.range;
pure void foo() {
    auto r = iota(10);
}
void main() {}


With DMD 2.052 gives the error:
test.d(3): Error: pure function 'foo' cannot call impure function 'iota'


The purity problem is here, from std.range module, enforce() is not pure:

struct Iota(N, S) if ((isIntegral!N || isPointer!N) && isIntegral!S) {
    private N current, pastLast;
    private S step;

    pure this(N current, N pastLast, S step)
    {
        enforce((current <= pastLast && step > 0) ||
                (current >= pastLast && step < 0));



This is part of std.exception.enforce(), a function with a lazy argument can't
be pure (and I think adding "pure lazy" to the D language can't help much
because most expressions given to enforce() aren't meant to be pure):

T enforce(T, string file = __FILE__, int line = __LINE__)
    (T value, lazy const(char)[] msg = null)
{
    if (!value) bailOut(file, line, msg);
    return value;
}


A solution is to remove the call to enforce() from the Iota constructor, and
replace it with a simpler if/throw.

See also bug 5124

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