class variable initialization

Steven Schveighoffer schveiguy at gmail.com
Sat Apr 15 15:47:40 UTC 2023


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 impossible. Can this be done in 
> some way?

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()
{
    Wrapper w;
    foo(w);
}
```

-Steve


More information about the Digitalmars-d-learn mailing list