| |
| Posted by monarchdodra@gmail.com in reply to bearophile_hugs@eml.cc | PermalinkReply |
|
monarchdodra@gmail.com
| http://d.puremagic.com/issues/show_bug.cgi?id=10718
monarchdodra@gmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |monarchdodra@gmail.com
--- Comment #1 from monarchdodra@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: -------
|