Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.algorithm.copy should keep the type of the characters it copies #9995

Open
dlangBugzillaToGithub opened this issue Jul 26, 2013 · 2 comments
Labels
Arch:x86 Issues specific to x86 OS:Windows Issues Specific to Windows Severity:Enhancement

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-07-26T05:11:55Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=10718

CC List

  • monarchdodra

Description

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[]
}
@dlangBugzillaToGithub
Copy link
Author

monarchdodra commented on 2013-07-26T05:45:10Z

(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.

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 2013-07-26T07:42:59Z

(In reply to comment #1)

> 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?

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arch:x86 Issues specific to x86 OS:Windows Issues Specific to Windows Severity:Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants