Re: class variable initialization

2023-04-15 Thread Ali Çehreli via Digitalmars-d-learn
On 4/15/23 09:06, NonNull wrote: >> struct Wrapper >> { >>Object x = new Object(); >>alias x this; >> } > Amazing, was this always so? At least for a long time. However, every Wrapper object will have a pointer to that single shared object. If you think that cost is unnecessary, you

Re: class variable initialization

2023-04-15 Thread NonNull via Digitalmars-d-learn
On Saturday, 15 April 2023 at 15:47:40 UTC, Steven Schveighoffer wrote: You can construct objects at compile time. If I understand your question properly: ```d struct Wrapper { Object x = new Object(); alias x this; } void foo(Object o) { assert(o !is null); } void main() {

Re: class variable initialization

2023-04-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/15/23 7:05 AM, NonNull wrote: I want a way to default initialize a class variable to a default object (e.g. by wrapping it in a struct, because initialization to null cannot be changed directly). Such a default object is of course not available at compile time which seems to make this

Re: class variable initialization

2023-04-15 Thread NonNull via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:17:19 UTC, Vijay Nayar wrote: I believe if you do initialization at the class declaration level, then every instance of the class shares the same instance, e.g.: ``` class Var {} class MyClass { Var var = new Var(); } void main() { MyClass c1 = new

Re: class variable initialization

2023-04-15 Thread Vijay Nayar via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:05:17 UTC, NonNull wrote: I want a way to default initialize a class variable to a default object (e.g. by wrapping it in a struct, because initialization to null cannot be changed directly). Such a default object is of course not available at compile time

class variable initialization

2023-04-15 Thread NonNull via Digitalmars-d-learn
I want a way to default initialize a class variable to a default object (e.g. by wrapping it in a struct, because initialization to null cannot be changed directly). Such a default object is of course not available at compile time which seems to make this impossible. Can this be done in some