Matlab specifies When [a] Set Method is Called
I ran into odd results, so I conducted a series of tests on the following class:
% @cScenInsts/cScenInsts.m
%-------------------------
classdef cScenInsts < matlab.mixin.Copyable
properties
TwinYrs = 5 % Default value
end % properties
methods
function o = cScenInsts( TwinYrs_in ) % Constructor
if exist('TwinYrs_in') && ~isempty( TwinYrs_in )
o.TwinYrs = TwinYrs_in ;
else
o.TwinYrs = o.TwinYrs ;
end % if
end % function cScenInsts()
function o = set.TwinYrs(o,TwinYrs_in)
o.TwinYrs = TwinYrs_in;
fprintf( '%s: Property TwinYrs = %g\n', mfilename, o.TwinYrs );
end % function set.TwinYrs
end % methods
end % classdef
The else block is superfluous in this minimum working example, but in real life, I want it to invoke the set method, which sets the values of other dependent parameters.
Using the class definition above, I ran into mixed results in terms of whether they seemed to follow the rules for when a set method is called.
UNEXPECTED behaviour in the following command: "Assigning a property to its default value that is specified in the class definition" shouldn't trigger set method, but it does.
>> o = cScenInsts;
cScenInsts: Property TwinYrs = 5
As well, "As an optimization, if MATLAB determines that a property value does not change as a result of an assignment referencing the property, MATLAB does not call the property set method". It seems that if the property is on the LHS and RHS of the assignment and the value doesn't change, the set method isn't called. This should apply above, as it is the else block that executes. The above result shows that it doesn't apply, which is the effect I seek. My anxt is that I don't know how much confidence I can have in the reliability of this behaviour in all circumstances when it seems to contradict the optimization provision.
EXPECTED behaviour in the following command:
>> o = cScenInsts(3);
cScenInsts: Property TwinYrs = 3
UNEXPECTED behaviour in the following command: "Assigning a property to its default value that is specified in the class definition" shouldn't trigger set method, but it does.
>> o = cScenInsts(5);
cScenInsts: Property TwinYrs = 5
EXPECTED behaviour in the following command:
>> o.TwinYrs = 3;
cScenInsts: Property TwinYrs = 3
EXPECTED behaviour in the following command, since the property's AbortSet attribute not set to true
>> o.TwinYrs = 3;
cScenInsts: Property TwinYrs = 3
UNEXPECTED behaviour in the following command: "Assigning a property to its default value that is specified in the class definition" shouldn't trigger set method, but it does.
>> o.TwinYrs = 5;
cScenInsts: Property TwinYrs = 5
AMBIGUOUS behaviour in the following command: On one hand, "[a]ssigning a property to its default value that is specified in the class definition" shouldn't trigger set method, but it does. On the other hand, it can also be considered expected behaviour, since the property's AbortSet attribute not set to true
>> o.TwinYrs = 5;
cScenInsts: Property TwinYrs = 5
Can anyone please explain the unexpected and/or ambiguous results?