Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 15, 2015 cannot implicitly convert char[] to string | ||||
---|---|---|---|---|
| ||||
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 string The line which fails is `p = std.string.format("s = %s", s);` I don't understand why I can't convert a char[] to string. |
August 15, 2015 Re: cannot implicitly convert char[] to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to vladde | 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:
>> runnable/test23.d(1219): Error: cannot implicitly convert expression (format("s = %s", s)) of type char[] to string
>
> The 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 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.
|
August 15, 2015 Re: cannot implicitly convert char[] to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | 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 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 string
>>
>> The 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 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.
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);
}
|
August 15, 2015 Re: cannot implicitly convert char[] to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to vladde | 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:
>> runnable/test23.d(1219): Error: cannot implicitly convert expression
>> (format("s = %s", s)) of type char[] to string
>
> The line which fails is `p = std.string.format("s = %s", s);`
>
> I don't understand why I can't convert a char[] to string.
Get rid of the 'in' in format's signature.
|
August 15, 2015 Re: cannot implicitly convert char[] to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 08/15/2015 01:54 PM, Timon Gehr wrote:
> 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:
>>> runnable/test23.d(1219): Error: cannot implicitly convert expression
>>> (format("s = %s", s)) of type char[] to string
>>
>> The line which fails is `p = std.string.format("s = %s", s);`
>>
>> I don't understand why I can't convert a char[] to string.
>
> Get rid of the 'in' in format's signature.
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.
|
August 15, 2015 Re: cannot implicitly convert char[] to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On 08/15/2015 04:45 AM, cym13 wrote: > 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 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 string >>> >>> The 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 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. 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 } > 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 Actually, 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 |
Copyright © 1999-2021 by the D Language Foundation