www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13882] New: Smarter std.algorithm.copy

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

          Issue ID: 13882
           Summary: Smarter std.algorithm.copy
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

Using std.algorithm.copy on two sequences that have compile-time length
compiles with no errors and always excepts at run-time (when the source is
longer than the destination):


void main() pure  safe nothrow  nogc {
    import std.algorithm: copy;
    int[6] source;
    int[5] dest;
    //copy(source, dest); // Not supported yet.
    copy(source[], dest[]); // No compilation error
}


This means the D type system here has compile-time knowledge, that we throw
away, turning a stronger static typing in an always run-time error.

So I suggest to std.algorithm.copy use the compile-time lengths when they are
available:

void main() pure  safe nothrow  nogc {
    import std.algorithm: copy;
    int[6] source;
    int[5] dest;
    copy(source, dest); // Compile-time length mismatch error.
}


Another usage example, using the improved std.range.takeExactly from Issue
13881 :

void main() pure  safe nothrow  nogc {
    import std.algorithm: copy;
    import std.range: takeExactly;
    int[6] source;
    int[5] dest;
    copy(source.takeExactly!5, dest[]); // OK
}

--
Dec 20 2014