On Monday, 2 April 2018 at 11:41:55 UTC, Eduard Staniloiu wrote:
On Monday, 2 April 2018 at 10:26:32 UTC, RazvanN wrote:
[...]

The compiler does an implicit conversion from the type `immutable B` to the type `B`. It is able to do safely do so because `struct B` has only
value types that can be copied.

The same thing happens for
    immutable x = 1;
    int y = x;

If you add an indirection in `struct B`, as such

struct B
{
    int a;
    int* p;
    /* ... */
}

Then you can see that the implicit conversion fails with
"onlineapp.d(22): Error: cannot implicitly convert expression B(0, null).this(2) of type immutable(B) to B"

I put the code at
https://run.dlang.io/gist/83756973012fcb4fec2660a39ffdad90&args=-unittest?args=-unittest

The same conversion rules that apply to built in qualified types applies to structs. I'm guessing the same is for classes but I haven't played that much with those so a second opinion would be nice :)

Cheers,
Edi

FYI sth. seems to have gone wrong with the current migration to authenticated Gists. Sorry about the inconvenience, the hotfix is already in the deploy queue and for this - the exported URL should have been:

https://run.dlang.io/gist/83756973012fcb4fec2660a39ffdad90?args=-unittest

Reply via email to