Comment #0 by dlang-bugzilla — 2017-07-05T15:09:42Z
Consider:
////////// test.d /////////
class C
{
Object o = new Object;
int[] arr = new int[4];
}
///////////////////////////
The code above currently compiles, but will behave in a surprising manner.
A user coming from another language may expect that there is a new instance of the object and array instantiated for every instance of C; however, this is not the case. In reality, .o and .arr will initially always point to the same data segment address for every instance of C.
This anti-feature not only allows breaking the type system, but has been a repeated source of confusion for D users (see e.g. issue 13890, issue 15604). This code is invalid and should be rejected.
Comment #1 by schveiguy — 2017-08-22T13:48:57Z
Duplicate of 2947?
Comment #2 by dfj1esp02 — 2017-08-22T14:05:10Z
Well, any type system violation is safety violation, so not sure about the keyword.
*** This issue has been marked as a duplicate of issue 2947 ***
Comment #3 by dlang-bugzilla — 2017-08-22T17:34:57Z
Thanks. Definitely not surprised this was filed before. I prefer the description on this bug but I'm a little biased ;)
Comment #4 by schveiguy — 2017-08-22T17:57:49Z
There's a certain extra weight attached to such a low bug number, especially when it has so many duplicates.
Comment #5 by dfj1esp02 — 2017-08-23T10:28:12Z
(In reply to Vladimir Panteleev from comment #3)
> I prefer the description on this bug but I'm a little biased ;)
This scenario needs dynamic initialization, and D requires an explicit constructor for it for whatever reason, so disallowing is the the most straightforward solution.