www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10718] New: std.algorithm.copy should keep the type of the characters it copies

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10718

           Summary: std.algorithm.copy should keep the type of the
                    characters it copies
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



Phobos Range-based functions see strings and char[] to arrays of dchar, but I
think that behavour is not good for std.algorithm.copy too:


import std.algorithm: copy;
void main() {
    char[5] arr1 = "hello", arr2;
    arr1[].copy(arr2[]); // Error.
    dchar[arr1.length] arr3;
    arr1[].copy(arr3[]); // OK.
}



(Ali from D learns says that he would expect copy to maintain the same type.)

See also:

import std.array: array;
void main() {
    char[5] arr = "hello";
    pragma(msg, typeof(arr.array)); // Prints: dchar[]
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 26 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10718


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com




 Phobos Range-based functions see strings and char[] to arrays of dchar, but I
 think that behavour is not good for std.algorithm.copy too:
 
 
 import std.algorithm: copy;
 void main() {
     char[5] arr1 = "hello", arr2;
     arr1[].copy(arr2[]); // Error.
     dchar[arr1.length] arr3;
     arr1[].copy(arr3[]); // OK.
 }
I think it would be better if copy (like every other phobos algorithm) used the knowledge that a dchar can be converted to a stream of characters, if needed. For example, according to your suggestion, if "std.algorithm.copy" simply iterated on the chars, then copying a unicode "string" into an array of dchar would fail catastrophically, and that is not acceptable at all. EG, this *must* work: string s = "日本語"; dchar[3] d; s.copy(d[]); On the other hand, we should be able to make this work: dchar[3] d = "日本語"d; char[] s = new char[](12); d.copy(s);
 (Ali from D learns says that he would expect copy to maintain the same type.)
I strongly disagree. std.algorithm operates on ranges. a string is a range of dchars.
 See also:
 
 import std.array: array;
 void main() {
     char[5] arr = "hello";
     pragma(msg, typeof(arr.array)); // Prints: dchar[]
 }
The documentation of array explicitly says it will behave this way, as it provides an RA range of the iterated range. That is part of its design, and can't change. That said, I had suggested (and partially worked on long ago) the option to specify what types you want array to produce. EG: auto myDoubles = array!double(myRangeOfInts); The same can be obtained with "myRangeOfInts.map!"cast(double)a"().array();", but it is not as convenient. Further more, this suggestion would allow this: dchar[3] d = "日本語"d; auto s = d[].array!(immutable char)(d); Which would not be possible via a map! workaround. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 26 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10718






 I strongly disagree. std.algorithm operates on ranges. a string is a range of
 dchars.
In code like this the map yield chars, yet copy converts them to dchar, is this good? import std.range, std.algorithm; void main() { char[5] arr; auto r = 5.iota.map!(i => cast(char)(i + 'a')); static assert(is(typeof(r.front) == char)); // OK r.copy(arr[]); // error } Thank you for your answers. Do you suggest to close down this issue? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 26 2013