The following snippet errors out:
---
struct S { }
class Test2 { this(S) pure {} }
void main()
{
auto test2 = new shared Test2(S());
auto test3 = new immutable Test2/(S());
}
---
test_shared.d(7): Error: mutable method test_shared.Test2.this is not callable u
sing a immutable object
test_shared.d(7): Error: incompatible types for ((new immutable(Test2)) / (S()))
: 'immutable(Test2)' and 'S'
However, since any instance of S is an independent copy, the resulting object is still unique and thus should be liable for immutable or shared object construction. Also, in addition to POD types, types containing only immutable references should be allowed.
Finally, shared references can also be allowed when constructing a shared object, but this is a different kind of "unique" or "isolated" concept - I call it "weakly isolated" in my library implementation [1] following the "weakly pure" nomenclature - so this may need some bigger changes.
[1] https://github.com/rejectedsoftware/vibe.d/blob/6c9efa2fdcef1797c84e58483410f262a2a82d67/source/vibe/core/concurrency.d#L958
Comment #1 by k.hara.pg — 2013-04-30T02:13:44Z
(In reply to comment #0)
> auto test3 = new immutable Test2 / (S()); // unnecessary '/'
auto test3 = new immutable Test2(S());
After the fix, the code would work.
Comment #2 by sludwig — 2013-04-30T02:24:01Z
Sorry, I was blind while preparing the test case. This is the correct one:
---
struct S { string str; }
class Test { S _s; this(S s) pure { _s = s; } }
void main()
{
auto test2 = new shared Test(S());
auto test3 = new immutable Test(S());
}
---
So POD indeed works right, but immutable (and shared) references are seemingly disallowed.
Comment #3 by k.hara.pg — 2013-05-04T00:43:03Z
(In reply to comment #2)
> Sorry, I was blind while preparing the test case. This is the correct one:
>
> ---
> struct S { string str; }
> class Test { S _s; this(S s) pure { _s = s; } }
>
> void main()
> {
> auto test2 = new shared Test(S());
> auto test3 = new immutable Test(S());
> }
> ---
>
> So POD indeed works right, but immutable (and shared) references are seemingly
> disallowed.
This is current dmd implementation limitation. In complex cases dmd cannot detect that the constructor generates unique object.
Comment #4 by robert.schadek — 2024-12-13T18:06:36Z