Lazily Initialized Variables that are Compatible with const/immutable

Timon Gehr timon.gehr at gmx.ch
Thu Nov 17 22:01:21 UTC 2022


On 17.11.22 22:49, Vijay Nayar wrote:
> On Thursday, 17 November 2022 at 15:33:47 UTC, razyk wrote:
>> On 17.11.22 13:04, MorteFeuille123 wrote:
>>>>
> 
> While the solutions from razyk and Ali take care of lazy initialization, 
> with Ali's solution also having the benefit of being thread-safe, none 
> of these methods are actually compatible with `const` or `immutable`, 
> meaning that in any case where lazy initialization is needed, you cannot 
> have a const class, you have to pick one or the other in the current setup.

Well, that's what `const` and `immutable` mean. If you need to restrict 
mutation in some other way, classic encapsulation works.

Note that it is not all that often appropriate to use those qualifiers 
when writing traditional OO code.


More information about the Digitalmars-d mailing list