On Sat, 24 Sep 2011 13:19:33 +0200, Peter Alexander <peter.alexander...@gmail.com> wrote:

Lazy loading and caching are the same thing.

struct Foo
{
     T m_lazyObj = null;
     bool m_isLoaded = false;

     T getObj()
     {
         if (!m_isLoaded)
         {
             m_lazyObj = func();
             m_isLoaded = true;
         }
         return m_lazyObj;
     }
}

Change m_lazyObj to m_cachedObj and m_isLoaded to m_isCached and you have caching.

This is too simple. If the system also rewrites all members to @properties that
sets m_isLoaded = true, it might work. Example:

struct S {
    int n;
lazy int otherN = () { return n + 2; } // compile time error here if you refer to lazily initialized members.
}

=>

struct S {
    int __n;
    int __otherN;
    bool __otherNloaded = false;


    @property
    int n( ) {
        return __n;
    }

    @property
    int n(int value) {
        if (__n != value) {
            __n = value;
            __otherNloaded = false;
        }
        return __n;
    }

    @property
    int otherN( ) {
       if (!__otherNloaded) {
           __otherN = () { return n + 2; }();
           __otherNloaded = true;
       }
       return __otherN;
    }
}

Now, the overhead might be troublesome, but this seems to me to work.

--
  Simen

Reply via email to