digitalmars.D.learn - Copying copy?
- bearophile (13/13) Jul 25 2013 Range-based functions see strings and char[] to arrays of dchar,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/16) Jul 25 2013 I agree. I would expect copy to maintain the same type.
- bearophile (4/5) Jul 26 2013 http://d.puremagic.com/issues/show_bug.cgi?id=10718
- monarch_dodra (8/13) Jul 26 2013 I reacted to your bug entry. I don't think copy's behavior should
- H. S. Teoh (11/29) Jul 26 2013 +1. I like this much better than breaking the "string is a range of
- bearophile (16/21) Jul 26 2013 Thank you very much for your comments. I am wrong all the time...
- monarch_dodra (12/33) Jul 26 2013 I suggest we change it to improvement. An different entry for
- Dicebot (9/16) Jul 26 2013 Not possible, front is 'dchar' for char arrays:
- bearophile (4/5) Jul 26 2013 So do I have to use foreach loops to fill a char[] lazily? :-)
- monarch_dodra (34/39) Jul 26 2013 You could also use appender. Appender supports it:
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
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, bearophileI agree. I would expect copy to maintain the same type. Ali
Jul 25 2013
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
On Friday, 26 July 2013 at 12:16:02 UTC, bearophile wrote:Ali Çehreli: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.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
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:+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.Ali Çehreli: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.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
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
On Friday, 26 July 2013 at 14:46:23 UTC, bearophile wrote:monarch_dodra: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.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
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
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
On Friday, 26 July 2013 at 15:31:06 UTC, bearophile wrote:Dicebot: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.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