[Issue 10718] std.algorithm.copy should keep the type of the characters it copies

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 26 05:45:12 PDT 2013


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


monarchdodra at gmail.com changed:

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


--- Comment #1 from monarchdodra at gmail.com 2013-07-26 05:45:10 PDT ---
(In reply to comment #0)
> 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: -------


More information about the Digitalmars-d-bugs mailing list