[Issue 11466] std.range.zip for nothrow and @safe functions

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Dec 17 11:23:35 PST 2014


https://issues.dlang.org/show_bug.cgi?id=11466

--- Comment #5 from bearophile_hugs at 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.

--


More information about the Digitalmars-d-bugs mailing list