Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Lazy Dependent Property
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Drew

Posts: 2
Registered: 8/31/12
Lazy Dependent Property
Posted: Apr 4, 2013 9:46 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

I have a dependent property that's somewhat expensive to calculate, so I don't want to re-calculate it unless I have to; i.e., unless the properties on which it depends change. Essentially, I want to do something like the snippet below, where the incrVal property would be expensive to calculate.

classdef MyClass < handle

properties
val
lazy = false
end

properties (Dependent)
incrVal
end

properties (Access = private)
savedIncrVal
end

methods
function obj = MyClass(val)
obj.val = val;
end

function set.val(obj, val)
obj.val = val;
obj.lazy = false; % I get a warning here.
end

function incrVal = get.incrVal(obj)
if obj.lazy
incrVal = obj.savedIncrVal;
else
incrVal = obj.val + 1; % This is the expensive part.
obj.savedIncrVal = incrVal;
obj.lazy = true;
end
end

end

end



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.