MATLAB Lazy Evaluation in Dependent Property
I have a class with a few properties that are dependent, but that I'd really like to calculate only once.
I've just about concluded that using lazy evaluation on a dependent class property in MATLAB is either impossible or a bad idea. The original plan was to have a private logical flag for each (public) property that needs updating and to have the constructor set it to true. Then when the property accessor was called, it would check that flag and calculate the value and store it (in another private property) only if required. If the flag were false, it would simply return a copy of the cached value.
I believe the difficulty lies in a constraint on property accessors, that is, that they leave other unrelated properties alone. In other words, a get.property(self) method can't change the state of the self object. Interestingly, this fails silently in my current class. (I.e., the neither the update flag nor the cached calculation results get set in the get. method, so the expensive calculation is run every time).
My suspi开发者_开发百科cion is that changing the lazy property from a public dependent property to a method with public GetAccess but private SetAccess would work. However, I don't like having to spoof the property convention in this way. I wish there were just a "lazy" property attribute that could do all this for me.
Am I missing something obvious? Are accessor methods for dependent class properties in MATLAB forbidden to change the class instance's state? If so, is defining what amounts to an accessor with a private side effect the least evil way of getting the behavior I want?
Edit: here's a test class...
classdef LazyTest
properties(Access = public)
% num to take factorial of
factoriand
end
properties(Access = public, Dependent)
factorial
end
properties(Access = private)
% logical flag
do_update_factorial
% old result
cached_factorial
end
methods
function self = LazyTest(factoriand)
self.factoriand = factoriand;
self.do_update_factorial = true;
end
end
methods
function result = get.factorial(self)
if self.do_update_factorial
self.cached_factorial = factorial(self.factoriand);
% pretend this is expensive
pause(0.5)
self.do_update_factorial = false
end
result = self.cached_factorial;
end
end
end
Run it with
close all; clear classes; clc
t = LazyTest(3)
t.factorial
for num = 1:10
tic
t.factoriand = num
t.factorial
toc
end
After inheriting from handle
the time drops substantially.
I assume you're using a value class. With a handle class (classdef myClass < handle
), which is passed by reference, you can easily modify the class from within a get-method. For example, I use what you propose in order to load data from file (if not yet loaded) or from a private, hidden property.
Note that using a lazy
dependent property the way you propose somewhat defeats the purpose of using a dependent property, i.e. the guarantee that your data is always up-to-date with the state of the properties it is derived from. Each time you change the other properties, your lazy property gets outdated.
You could (should) add a set-method to all other properties that sets the private property to empty (isempty(obj.myPrivateProperty)
is the "logical flag" you need to know whether you have to calculate). But if you do that, why not just have the set-methods call some update method that updates/recalculates all "dependent" properties right away?
精彩评论