www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Copying copy?

reply "bearophile" <bearophileHUGS lycos.com> writes:
Range-based functions see strings and char[] to arrays of dchar, 
but is that behavour good for std.algorithm.copy too?

I find this a bit silly:


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


What do you think?

Bye,
bearophile
Jul 25 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/25/2013 04:55 PM, bearophile wrote:
 Range-based functions see strings and char[] to arrays of dchar, but is
 that behavour good for std.algorithm.copy too?

 I find this a bit silly:


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


 What do you think?

 Bye,
 bearophile
I agree. I would expect copy to maintain the same type. Ali
Jul 25 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 I agree. I would expect copy to maintain the same type.
http://d.puremagic.com/issues/show_bug.cgi?id=10718 Bye, bearophile
Jul 26 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 26 July 2013 at 12:16:02 UTC, bearophile wrote:
 Ali Çehreli:

 I agree. I would expect copy to maintain the same type.
http://d.puremagic.com/issues/show_bug.cgi?id=10718 Bye, bearophile
I reacted to your bug entry. I don't think copy's behavior should be *changed*. That would violate the entire "a string is a range of dchar" thing. However, copy could be improved with the knowledge that a dchar can be streamed into a series of chars (EG, the way an appender!(char[]) can handle taking a dchar), and improved to handle cases it didn't handle before.
Jul 26 2013
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Jul 26, 2013 at 02:48:05PM +0200, monarch_dodra wrote:
 On Friday, 26 July 2013 at 12:16:02 UTC, bearophile wrote:
Ali Çehreli:

I agree. I would expect copy to maintain the same type.
http://d.puremagic.com/issues/show_bug.cgi?id=10718 Bye, bearophile
I reacted to your bug entry. I don't think copy's behavior should be *changed*. That would violate the entire "a string is a range of dchar" thing. However, copy could be improved with the knowledge that a dchar can be streamed into a series of chars (EG, the way an appender!(char[]) can handle taking a dchar), and improved to handle cases it didn't handle before.
+1. I like this much better than breaking the "string is a range of dchar" abstraction, which is built into Phobos in many places. Much better to say that it's OK to write dchars into string, because they can be recoded into UTF-8. This does make me wonder, should we support dchar -> string conversion in general? I see that appender supports that, which is great, but what about language-level support (say, string ~ dchar)? T -- Tech-savvy: euphemism for nerdy.
Jul 26 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
monarch_dodra:

 I reacted to your bug entry.
Thank you very much for your comments. I am wrong all the time... I have written a small answer.
 However, copy could be improved with the knowledge that a dchar 
 can be streamed into a series of chars (EG, the way an 
 appender!(char[]) can handle taking a dchar), and improved to 
 handle cases it didn't handle before.
Is your improvement enough to solve this problem of mine? 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 } Is that Issue 10718 to be closed, or do you suggest to turn it into an enhancement request for your proposed improvements of "copy" and "array"? Bye, bearophile
Jul 26 2013
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 26 July 2013 at 14:46:23 UTC, bearophile wrote:
 monarch_dodra:

 I reacted to your bug entry.
Thank you very much for your comments. I am wrong all the time... I have written a small answer.
 However, copy could be improved with the knowledge that a 
 dchar can be streamed into a series of chars (EG, the way an 
 appender!(char[]) can handle taking a dchar), and improved to 
 handle cases it didn't handle before.
Is your improvement enough to solve this problem of mine? 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 } Is that Issue 10718 to be closed, or do you suggest to turn it into an enhancement request for your proposed improvements of "copy" and "array"? Bye, bearophile
I suggest we change it to improvement. An different entry for "array" should be made though IMO. Your example code (seems) to be failing because (apparently) "char[]" is not an output range for "dchar". eg: "char[].put(dchar)" won't compile, so that needs to be fixed first. This improvement had been submitted before, and I had worked on it too, but it kind of fell on the side with more important fixes: https://github.com/D-Programming-Language/phobos/pull/1000 I can put this back on my "todo" list, but to be honest, I have a few more important things I'd like to work on first.
Jul 26 2013
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 26 July 2013 at 14:46:23 UTC, bearophile wrote:
 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
 }
Not possible, front is 'dchar' for char arrays: import std.array; void main() { char[5] arr; pragma(msg, typeof(arr.front)); // dchar }
Jul 26 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Dicebot:

 Not possible, front is 'dchar' for char arrays:
So do I have to use foreach loops to fill a char[] lazily? :-) Bye, bearophile
Jul 26 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 26 July 2013 at 15:31:06 UTC, bearophile wrote:
 Dicebot:

 Not possible, front is 'dchar' for char arrays:
So do I have to use foreach loops to fill a char[] lazily? :-) Bye, bearophile
You could also use appender. Appender supports it: //---- import std.array, std.algorithm, std.stdio; void main() { dchar[3] d = "日本語"; string s = "I speak "; auto app = appender(s); app.put(d[]); writeln(app.data); } //---- That said, I'd suggest to not pass an array to appender when building it: the current implementation makes a lot of assumptions about the state of its internal buffer, which end up being wrong when you pass an array as above. These assumption can lead to some potentially very nasty effects, including plain old segfault. Today, I'd suggest instead writing it as: //---- import std.array, std.algorithm, std.stdio; void main() { dchar[3] d = "日本語"; string s = "I speak "; auto app = Appender!(string)(); app.put(s); app.put(d[]); s = app.data; writeln(s); } //---- Doing this is safe.
Jul 26 2013