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

Reply via email to