struct: default construction or lazy initialization.

2017-01-31 Thread bitwise via Digitalmars-d-learn
Unless I'm missing something, it seems that neither of these are actually possible. Consider an object which needs internal state to function. The obvious answer is to create it in the constructor: struct Foo(T) { T* payload; this() { payload = cast(T*)malloc(T.sizeof);

Re: struct: default construction or lazy initialization.

2017-01-31 Thread Ali Çehreli via Digitalmars-d-learn
On 01/31/2017 03:15 PM, bitwise wrote: > If the object is defined at module scope as shared static immutable Yes, the situation is different from C++ but it's always possible to call a function (which constructor is one) to make the object. It is indeed possible to initialize immutable object

Re: struct: default construction or lazy initialization.

2017-01-31 Thread bitwise via Digitalmars-d-learn
On Tuesday, 31 January 2017 at 23:52:31 UTC, Ali Çehreli wrote: On 01/31/2017 03:15 PM, bitwise wrote: [...] Thanks for the response, but this doesn't really solve the problem. > If the object is defined at module scope as shared static > immutable It is indeed possible to initialize immuta

Re: struct: default construction or lazy initialization.

2017-01-31 Thread bitwise via Digitalmars-d-learn
C#'s "Dispose" pattern comes to mind here. You don't leak memory, you just leak file handles and graphics resources instead when you forget to explicitly call Dispose().

Re: struct: default construction or lazy initialization.

2017-01-31 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 1 February 2017 at 00:43:39 UTC, bitwise wrote: Container!int c; // = Container!int() -> can't do this. Can you live with Container!int c = Container!int.create(); because D supports that and can force the issue with `@disable this();` which causes compilation to fail any place

Re: struct: default construction or lazy initialization.

2017-02-01 Thread bitwise via Digitalmars-d-learn
On Wednesday, 1 February 2017 at 01:52:40 UTC, Adam D. Ruppe wrote: On Wednesday, 1 February 2017 at 00:43:39 UTC, bitwise wrote: Container!int c; // = Container!int() -> can't do this. Can you live with Container!int c = Container!int.create(); because D supports that and can force the issu

Re: struct: default construction or lazy initialization.

2017-02-01 Thread kinke via Digitalmars-d-learn
On Wednesday, 1 February 2017 at 23:02:11 UTC, bitwise wrote: On Wednesday, 1 February 2017 at 01:52:40 UTC, Adam D. Ruppe wrote: On Wednesday, 1 February 2017 at 00:43:39 UTC, bitwise wrote: Container!int c; // = Container!int() -> can't do this. Can you live with Container!int c = Containe

Re: struct: default construction or lazy initialization.

2017-02-01 Thread bitwise via Digitalmars-d-learn
On Wednesday, 1 February 2017 at 23:24:27 UTC, kinke wrote: It's not that bad. D just doesn't support a default ctor for structs at all and simply initializes each instance with T.init. Your `s2` initialization is most likely seen as explicit default initialization (again with T.init). Destruc

Re: struct: default construction or lazy initialization.

2017-02-01 Thread kinke via Digitalmars-d-learn
On Wednesday, 1 February 2017 at 23:32:12 UTC, bitwise wrote: On Wednesday, 1 February 2017 at 23:24:27 UTC, kinke wrote: It's not that bad. D just doesn't support a default ctor for structs at all and simply initializes each instance with T.init. Your `s2` initialization is most likely seen as