www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't put a const(char)[] into a char[]

reply Steven Schveighoffer <schveiguy yahoo.com> writes:
I just came across this. Can't believe there's not been more people 
finding this:

char[] buf = new char[100];
buf.formattedWrite("This is a number: %s", 5); // Error cannot put a 
const(char)[] into a char[]


OK, challenge accepted!

buf[0 .. myName.length] = myName[]; // Hey phobos, look, this is easy!

But no, of course -- autodecoding. >:(

https://issues.dlang.org/show_bug.cgi?id=18790

-Steve
Apr 21 2018
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 21 April 2018 at 22:34:39 UTC, Steven Schveighoffer 
wrote:
 I just came across this. Can't believe there's not been more 
 people finding this:

 char[] buf = new char[100];
 buf.formattedWrite("This is a number: %s", 5); // Error cannot 
 put a const(char)[] into a char[]


 OK, challenge accepted!

 buf[0 .. myName.length] = myName[]; // Hey phobos, look, this 
 is easy!

 But no, of course -- autodecoding. >:(

 https://issues.dlang.org/show_bug.cgi?id=18790

 -Steve
"Use .representation" would be the official answer for the second hack. But this should absolutely work. It looks this looks the same thing as https://issues.dlang.org/show_bug.cgi?id=18472#c10 --- enum s = "%1$s,%2$s".format("foo","bar"); --- phobos/std/range/primitives.d(405): Error: static assert: "Cannot put a const(char)[] into a Appender!string." phobos/std/format.d(1184): instantiated from here: put!(Appender!string, const(char)[]) /phobos/std/format.d(473): instantiated from here: writeUpToNextSpec!(Appender!string) phobos/std/format.d(6168): instantiated from here: formattedWrite!(Appender!string, char, string, string) main.d(5): instantiated from here: format!(char, string, string) 18472 broke compilation of DCompute under -betterC when upgrading LDC to a newer DMDFE, I "fixed" it by not compiling with -betterC but then Mike Franklin (thanks!) discovered the above error after fixing the -betterC side of things. I'm going to be extremely unhappy if I can't compile dcompute at all with the latest LDC. IMO this is a priority 1 regression.
Apr 21 2018
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/21/18 7:30 PM, Nicholas Wilson wrote:

 "Use .representation" would be the official answer for the second hack.
I have since discovered that I can use byCodeUnit, but I'm still annoyed that this is required. I *think* that this is the only problem: https://github.com/dlang/phobos/blob/master/std/range/primitives.d#L365 Essentially, here is the block: //Optional optimization block for straight up array to array copy. else static if (isDynamicArray!R && !isNarrowString!R && isDynamicArray!E && is(typeof(r[] = e[]))) { immutable len = e.length; r[0 .. len] = e[]; r = r[len .. $]; } If we just take out the !isNarrowString!R, I think it will just work, and probably cause no problems. I have NO IDEA why we are purposely shooting ourselves in the foot here. I'm going to at least try it (PR forthcoming). There are a few bits inside put that are supposed to cater to narrow strings, I'm not sure why it's not working. -Steve
Apr 21 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/21/18 7:56 PM, Steven Schveighoffer wrote:

 I'm going to at least try it (PR forthcoming).
https://github.com/dlang/phobos/pull/6471 -Steve
Apr 21 2018