Possible way to achieve lazy loading with const objects

Peter Alexander peter.alexander.au at gmail.com
Sun Sep 25 05:52:56 PDT 2011


On 24/09/11 6:53 PM, Timon Gehr wrote:
> On 09/24/2011 07:21 PM, Peter Alexander wrote:
>> On 24/09/11 12:47 PM, Jonathan M Davis wrote:
>>> On Saturday, September 24, 2011 12:19:33 Peter Alexander wrote:
>>>> Lazy loading and caching are the same thing.
>>>
>>> No. Caching is more general. Lazy loading is explicitly one load and has
>>> different characteristics, whereas is caching can have multiple loads.
>>
>> Yes, you are right. Sorry.
>>
>>> The problem with this is that with a single load, you can guarantee
>>> constness,
>>> but once you have multiple, you can't. Unless you can find a mechism
>>> which
>>> _guarantees_ logical constness while changing the variable multiple
>>> times,
>>> then you can't guarantee constness, and it doesn't work. Also,
>>> whatever the
>>> mechanism is, it _has_ to work in the face of immutable, which may or
>>> may not
>>> be harder with a more general caching mechanism.
>>>
>>> This particular solution jsn't even _trying_ to solve the caching
>>> problem. I
>>> seriously question that that's at all solvable without breaking const.
>>> The
>>> whole reason that this could work is because it does the load only
>>> once, which
>>> therefore guarantees that the value never changes and therefore never
>>> breaks
>>> const. This is only attempting to solve the lazy-loading issue, not the
>>> general caching issue.
>>>
>>> If you can come up with a way to do general caching without breaking
>>> const,
>>> then that's great. But all I've been able to come up with is a
>>> potential means
>>> of doing lazy loading.
>>>
>>> But really, my real question here is whether this scheme is really
>>> feasible or
>>> whether I've missed something. If it's feasible, then _maybe_ it's
>>> possible to
>>> build on it somehow to come up with a general caching mechanism
>>> (though I
>>> doubt it), but if it's _not_, then obviously this sort of approach
>>> isn't going
>>> to be able to do a single lazy load, let alone be expanded somehow to
>>> make
>>> general caching possible.
>>>
>>> - Jonathan M Davis
>>
>> I think your approach to lazy loading works.
>>
>> However, I'm not sure I see the point of just allowing one kind of
>> enforced logical const. Logical const can't be enforced in general, and
>> I think in this case a partial attempt is as good as no attempt.
>
> I disagree. Lazy values are useful for many purposes.

Sounds like we agree to me :-)




More information about the Digitalmars-d mailing list