Why does property size validation induce a constructor call?

Viewed 39

I have encountered a behaviour in Matlab that I cannot quite get my head around. I'm writing a handle class called PlottingUI which takes a SolitonSolution as its only input (where SolitonSolution is a value class that I have also written). It looks something like this:

classdef PlottingUI < handle
    properties (Access = private)
        ss(1,1) SolitonSolution
        ...
    end
    methods (Access = public)
        function obj = PlottingUI(ss)
            obj.ss = ss;
            ...
        end
        ...
    end
    ...
end

Note especially the (1,1) in the properties validation block. It might seem unnecesary, but it is intended to stop the user from trying to pass, say, an array of SolitonSolution objects. It certainly should not hurt to have it. But now, if I attempt to instantiate a PlottingUI object with

...
ui = PlottingUI(ss);

where ss is a previously instantiated SolitonSolution object, then I get an error like this:

Error using SolitonSolution (line 14)
Not enough input arguments.

Error in testUI (line 17)
ui = PlottingUI(ss);

testUI is just the script name. The line in SolitonSolution that it gets stuck on is an narginchk call in its constructor. The call stack at this moment is

SolitonSolution.SolitonSolution
testUI
Base

so this happens before entering the PlottingUI constructor.

The really weird thing is that if I remove the size specification (1,1) in the PlottingUI properties block the error disappears.

So it seems like the size specification (1,1) induces a call to the SolitonSolution constructor (with no arguments) before calling the PlottingUI constructor. But why?

0 Answers
Related