digitalmars.D.learn - Class member initialization with new points to a single instance?
- Gregor =?UTF-8?B?TcO8Y2ts?= (13/13) Jun 09 2021 Consider the following code:
- Adam D Ruppe (8/11) Jun 09 2021 Yes, it is expected if you are familiar with the spec.
- evilrat (6/19) Jun 09 2021 By design.
- Gregor =?UTF-8?B?TcO8Y2ts?= (6/30) Jun 09 2021 My two cents:
- mw (3/36) Jun 09 2021 Can we add (enforcement) `static` in this case, to make it clear.
Consider the following code: ```d class Foo { } class Bar { Foo foo = new Foo(); } void main() { Bar b1 = new Bar(); Bar b2 = new Bar(); assert(b1.foo != b2.foo); } ``` The assert fails. This is completely surprising to me. Is this actually expected?
Jun 09 2021
On Wednesday, 9 June 2021 at 17:56:24 UTC, Gregor Mückl wrote:class Bar { Foo foo = new Foo(); }This is a static initialization....The assert fails. This is completely surprising to me. Is this actually expected?Yes, it is expected if you are familiar with the spec. All member = x things inside a class or struct declaration are static initializers. These are CTFE'd into the same form as a literal. So there's one instance of Foo there which is assigned to the object member before the constructor runs. For a runtime initialization, you use a constructor.
Jun 09 2021
On Wednesday, 9 June 2021 at 17:56:24 UTC, Gregor Mückl wrote:Consider the following code: ```d class Foo { } class Bar { Foo foo = new Foo(); } void main() { Bar b1 = new Bar(); Bar b2 = new Bar(); assert(b1.foo != b2.foo); } ``` The assert fails. This is completely surprising to me. Is this actually expected?By design. What you see is CTFE instance shared through class member initializer. Use Bar ctor instead if you want them to be unique. Yep, confusing for the first time.
Jun 09 2021
On Wednesday, 9 June 2021 at 18:04:54 UTC, evilrat wrote:On Wednesday, 9 June 2021 at 17:56:24 UTC, Gregor Mückl wrote:My two cents: I think this should be changed because all other common languages that support equivalent syntax have settled on runtime initialization for this. But it's probably too deeply embedded into the language now. :(Consider the following code: ```d class Foo { } class Bar { Foo foo = new Foo(); } void main() { Bar b1 = new Bar(); Bar b2 = new Bar(); assert(b1.foo != b2.foo); } ``` The assert fails. This is completely surprising to me. Is this actually expected?By design. What you see is CTFE instance shared through class member initializer. Use Bar ctor instead if you want them to be unique. Yep, confusing for the first time.
Jun 09 2021
On Wednesday, 9 June 2021 at 18:12:01 UTC, Gregor Mückl wrote:On Wednesday, 9 June 2021 at 18:04:54 UTC, evilrat wrote:Can we add (enforcement) `static` in this case, to make it clear. It's so confusing.On Wednesday, 9 June 2021 at 17:56:24 UTC, Gregor Mückl wrote:My two cents: I think this should be changed because all other common languages that support equivalent syntax have settled on runtime initialization for this. But it's probably too deeply embedded into the language now. :(Consider the following code: ```d class Foo { } class Bar { Foo foo = new Foo(); } void main() { Bar b1 = new Bar(); Bar b2 = new Bar(); assert(b1.foo != b2.foo); } ``` The assert fails. This is completely surprising to me. Is this actually expected?By design. What you see is CTFE instance shared through class member initializer. Use Bar ctor instead if you want them to be unique. Yep, confusing for the first time.
Jun 09 2021