Possible way to achieve lazy loading with const objects

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 26 07:35:29 PDT 2011


On Mon, 26 Sep 2011 09:44:31 -0400, Regan Heath <regan at netmail.co.nz>  
wrote:

> On Mon, 26 Sep 2011 13:01:29 +0100, Steven Schveighoffer  
> <schveiguy at yahoo.com> wrote:
>> On Sat, 24 Sep 2011 00:11:52 -0400, Jonathan M Davis
>>> 6.  If the S being constructed is shared or immutable and __varProp is  
>>> not
>>> called in the constructor, then __varProp is called immediately after  
>>> the
>>> constructor (or at the end of the constructor if that works better for  
>>> the
>>> compiler).
>>
>> Why?  What if the calculation is very expensive, and you never access  
>> var?
>>
>> Besides, we can already pro-actively initialize data in an immutable  
>> constructor, what is the benefit here?
>
> I think this is to avoid threading issues, like double checked locking  
> problems etc.

My point is, can't I do this now?

struct S
{
    int var;
    immtuable this() { var = func(); }
    const int func() {...}
}

vs

struct S
{
    lazy int var = func();
    const int func() {...}
}

If you aren't going to *actually* lazily initialize a variable, what is  
the point of all this?  I can non-lazily initialize a variable without any  
new language features.

-Steve


More information about the Digitalmars-d mailing list