digitalmars.D.learn - cannot implicitly convert char[] to string
- vladde (5/7) Aug 15 2015 I made a PR to phobos where I modified `std.format.format`.
- cym13 (4/11) Aug 15 2015 I think it has to do with the fact that string is an alias to
- cym13 (24/38) Aug 15 2015 I phrased it completely wrong, an example will be better :
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (18/45) Aug 15 2015 That's actually correct (for reference types). As long as there is no
- Timon Gehr (2/9) Aug 15 2015 Get rid of the 'in' in format's signature.
- Timon Gehr (4/16) Aug 15 2015 Oh, I see, this is by design (which I don't like, but OK.)
I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message:runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to stringThe line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string.
Aug 15 2015
On Saturday, 15 August 2015 at 11:25:20 UTC, vladde wrote:I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message:I think it has to do with the fact that string is an alias to immutable(char)[] and you can't implicitely cast an immutable to a regular variable.runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to stringThe line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string.
Aug 15 2015
On Saturday, 15 August 2015 at 11:34:01 UTC, cym13 wrote:On Saturday, 15 August 2015 at 11:25:20 UTC, vladde wrote:I phrased it completely wrong, an example will be better : import std.stdio; void fun(immutable(int)[] i) { i.writeln(); } void main() { int[] i = [42]; fun(i); } Will not compile because there is no certainty that fun() won't change the array while the following will work import std.stdio; void fun(immutable(int)[] i) { i.writeln(); } void main() { int[] i = [42]; fun(i.dup); immutable(int)[] j = [42]; fun(j); immutable(int[]) k = [42]; fun(k); }I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message:I think it has to do with the fact that string is an alias to immutable(char)[] and you can't implicitely cast an immutable to a regular variable.runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to stringThe line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string.
Aug 15 2015
On 08/15/2015 04:45 AM, cym13 wrote:On Saturday, 15 August 2015 at 11:34:01 UTC, cym13 wrote:That's actually correct (for reference types). As long as there is no indirection, a value type can be casted implicitly: struct S { int i; } void main() { auto i = immutable(S)(); auto m = S(); m = i; // copied; fine }On Saturday, 15 August 2015 at 11:25:20 UTC, vladde wrote:I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message:I think it has to do with the fact that string is an alias to immutable(char)[] and you can't implicitely cast an immutable to a regular variable.runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to stringThe line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string.I phrased it completely wrong, an example will be better : import std.stdio; void fun(immutable(int)[] i) { i.writeln(); } void main() { int[] i = [42]; fun(i); } Will not compile because there is no certainty that fun() won't change the arrayActually, that's the job of 'const'. Here, it is not fun's ability to mutate but the caller's. 'immutable' on a function interface means a requirement: The caller *must* provide immutable data so that the function can rely on it not being changed by anyone. Ali
Aug 15 2015
On 08/15/2015 01:25 PM, vladde wrote:I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message:Get rid of the 'in' in format's signature.runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to stringThe line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string.
Aug 15 2015
On 08/15/2015 01:54 PM, Timon Gehr wrote:On 08/15/2015 01:25 PM, vladde wrote:Oh, I see, this is by design (which I don't like, but OK.) The reason the conversion does not go through is that format is not marked as pure.I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message:Get rid of the 'in' in format's signature.runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to stringThe line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string.
Aug 15 2015