In a class, I have a dependent property that's expensive to calculate. It only depends on one other property, call it "value" in the class, so I'm trying to prevent it from re-calculating unless value changes.
The snippet below works, but it shows a warning about setting the property value for lazy in the set method for value. Is there a better way to do this?
classdef MyClass < handle properties value; end properties (Dependent) output; end properties (Access = private) lazy = false; cachedOutput; end methods function obj = MyClass(value) obj.value = value; end function set.value(obj, value) obj.lazy = false; % warning here
obj.value = value; end function res = get.output(obj) if obj.lazy res = obj.cachedOutput; else res = expensive_function(obj.value); obj.cachedOutput = res; obj.lazy = true; end end endendfunction res = expensive_function(value) res = value + 1;end
Best Answer