I have a class which has some properties and 2 are related, in the example called param1, param2
. They are independent, just constrained. param2
must be as large or larger than param1
and must always exist if param1
does. Code in question is something like:
function set.param1(obj, input)
disp('setting param1')
obj.param1 = input;
if (isempty(obj.param2) || obj.param2 < obj.param1) % Warning on param2
obj.param2 = obj.param1; % Warning on param2
end
end
Similar code for the set.param2
.
Code works fine and I cannot see any better way to do it. The problem - it produces warning "a set method..." like mentioned in title. I suppressed them for the lack of better solution.
Is there a better way to achieve this functionality and without warnings? Obviously not a hacky "solution" like a hidden function SetParam2
:
function SetParam2(obj, input)
obj.param2 = input;
end
which confuses editor just enough it doesn't complain.
You could use two layers of properties
Dependent
The similar technique is used here in the documentation: Avoid Property Initialization Order Dependency.
The trick here:
privateParam1
andprivateParam2
stores the two values. The get and set only implemented for the exposed propertiesparam1
andparam2
: theget
returns simply the inner property and in theset
both of them can be used without the analyzer warning as they are marked asDependent
.