Possible way to achieve lazy loading with const objects
Jonathan M Davis
jmdavisProg at gmx.com
Sun Sep 25 18:03:23 PDT 2011
On Sunday, September 25, 2011 17:46:00 Walter Bright wrote:
> On 9/23/2011 9:11 PM, Jonathan M Davis wrote:
> > Okay. I'm not saying that we should necessarily implement this. I'm just
> > looking to air out an idea here and see if there are any technical
> > reasons why it can't be done or is unreasonable.
>
> Andrei and I talked about this some time back. Where it ran aground was
> Andrei wanted a way to mark the object as 'dirty' so it would get reloaded.
> We couldn't find a way that didn't look like a mess.
>
> It also has problems if you try and add 'const' to it, because it is under
> the hood not const. Nor is it immutable or thread safe.
Well, that's why I tried to find a solution specifically for lazy loading as
opposed to a more general caching mechanism. Using const with a more general
caching mechanism seems like it would be _really_ hard to do if not outright
impossible, but it at least seems like it may be possible to do it with a
single, lazy load, since the value never changes once it's been set. And the
idea with immutability and shared was that they would force eager-loading so
that they wouldn't be a problem. The whole thing becomes vastly more
complicated if you try and have a more general caching mechanism.
On the whole, it looks to me like my idea could work, but there may be
complications with regards to what you can allow in the initializer function
for the lazy member variable. Sorting those out could render the idea more or
less useless, and even if it works perfectly exactly as I suggested, I don't
know that what it adds merits the extra complexity that it requires.
It would be very nice if we could expand const to be able to allow for some
level of controlled, logical constness, but the ultimate problem is finding a
way to control it. If it could be controlled, then there are probably ways to
make it work with immutable (such as eliminating the caching in the case of
immutable), but it's a very difficult problem, and I'm not at all convinced that
it's ultimately solvable. I was just hoping to find a partial solution.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list