Comment #0 by verylonglogin.reg — 2012-11-03T07:59:49Z
A proposal to fix Issue 8956 - Ability to break typesystem with constructor/postblit/destructor
First, lets make them qualifier-overloadable.
Then let we have such struct:
---
class C { }
struct S0
{ int i; immutable int* p; }
struct S1
{ int i; int* p; }
struct S2
{ S1 s1; }
struct S
{
int i;
int* p;
int[] darr;
int[1] sarr;
C c;
S0* pS0;
S1 s1;
S1[1] sarrS1;
S1* pS1;
S2 s2;
this(this) inout // e.g. a postblit
{
// What will we see here?
}
}
---
In an `inout` ctor/postblit/dtor we will see fields as:
* For pointers and dynamic arrays, as a result of `cast()`:
inout(int)* p;
inout(int)[] arr;
inout(S0)* pS0;
inout(S1)* pS1;
* For class references
* require Issue 5325 - Mutable references to const/immutable/shared classes
inout(C)ref c;
* For types where `inout(T)` is assignable to `T` (primitive types, some structs/unions):
int i;
int[1] i;
S0 s0;
* For all other types (structs/unions):
inout(S1) s1;
inout(S1[1]) sarrS1;
inout(S2) s2;
* But assignment is allowed:
s1 = inout(S1)();
* Assignment by index or property is allowed if it writes to (&this)[0 .. 1] memory:
sarrS1[0] = inout(S1)();
s2.s1 = inout(S1)();
s2.s1.i = 5;
* For its fields that are in (&this)[0 .. 1] memory same type-replacement rules apply:
is(typeof(s2.s1.i) == int)
This proposal can be consolidated in some analog of `inout(T)ref` from Issue 5325 but for structs with same syntax and one simple rule: it is mutable if it is in (&this)[0 .. 1] memory.
You are welcome to find mistakes, improve, destroy or make a counter-proposal!
Comment #1 by robert.schadek — 2024-12-13T18:02:22Z