Jump to page: 1 2
Thread overview
[Issue 11466] std.range.zip for nothrow and @safe functions
Jun 08, 2016
Walter Bright
[Issue 11466] std.range.zip is not nothrow
Jun 08, 2016
Walter Bright
Dec 19, 2019
berni44
June 18, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx

--
June 18, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe

--
December 04, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #1 from bearophile_hugs@eml.cc ---
In dmd 2.067alpha this code compiles:


import std.range: zip;
void foo1(int[] data) @safe {
    foreach (pair; zip(data, data)) {}
}
void foo2(in int[] data) @safe {
    foreach (pair; zip(data, data)) {}
}
void main() {}

--
December 04, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #2 from hsteoh@quickfur.ath.cx ---
So this bug can be closed?

--
December 04, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #3 from bearophile_hugs@eml.cc ---
(In reply to hsteoh from comment #2)
> So this bug can be closed?

No, it can't because this fails still:

import std.range: zip;
void main() nothrow {
    foreach (p; zip([10], [20])) {}
}

--
December 17, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #4 from hsteoh@quickfur.ath.cx ---
Zip.empty and Zip.popFront call enforce, which is not nothrow. This can't be eliminated because the runtime length of the ranges passed to zip() is not known, and zip() will throw an exception if unequal-length ranges are passed to it.

I don't know how to resolve this, since the only way to make Zip nothrow is to change enforce to assert, but that means you'll get array bounds violations at runtime if the input arguments have unequal lengths.

--
December 17, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #5 from bearophile_hugs@eml.cc ---
(In reply to hsteoh from comment #4)

> zip() will throw an exception if unequal-length ranges are passed to it.

This is not true, this doesn't throw:

void main() {
    import std.range: zip, StoppingPolicy;
    int[] a = [1];
    int[] b = [1, 2];
    foreach (pair; zip(a, b)) {}
    foreach (pair; zip(StoppingPolicy.shortest, a, b)) {}
    foreach (pair; zip(StoppingPolicy.longest, a, b)) {}
}


Only this one throws:

void main() {
    import std.range: zip, StoppingPolicy;
    int[] a = [1];
    int[] b = [1, 2];
    foreach (pair; zip(StoppingPolicy.requireSameLength, a, b)) {}
}



> I don't know how to resolve this,

The solution is to give StoppingPolicy as template argument to zip() instead of
giving it as run-time value. This allows to specialize zip() at compile-time,
allowing zip() with StoppingPolicy.shortest (that is the default) and with
StoppingPolicy.longest to be nothrow. And leaving only the quite rarely used
zip() with StoppingPolicy.requireSameLength not nothrow.

For API compatibility you can even leave both ways to give StoppingPolicy to zip, or you can slowly deprecate the old way.

--
December 17, 2014
https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #6 from hsteoh@quickfur.ath.cx ---
Good idea.

However, it will break compatibility with the current Zip struct, which unfortunately is public so some user code out there may be naming it directly. Looks like we'll have to introduce a different overload of Zip for the compile-time StoppingPolicy variant. Code duplication galore. :-(

--
June 08, 2016
https://issues.dlang.org/show_bug.cgi?id=11466

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> ---
Since the @safe version compiles successfully, I'm going to remove the 'safe' keyword and @safe from the title.

--
June 08, 2016
https://issues.dlang.org/show_bug.cgi?id=11466

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|safe                        |EH
            Summary|std.range.zip for nothrow   |std.range.zip is not
                   |and @safe functions         |nothrow

--
« First   ‹ Prev
1 2