Method chaining unexpected behaviour?
7 views (last 30 days)
Benjamin Chen on 19 Dec 2017
I have noticed some unexpected behaviour with method chaining. Specifically I have a class A from which I want to call the static method to create class B:
function b = getB(args)
b = B(args);
And then I want to use class B to do something e.g. use the print() method, where class B is:
function self = B(args)
self.Property1 = args;
I would thus also expect
to succeed, however, it gives the error:
Not enough input arguments.
Error in A.getB (line 5)
b = B(args);
Would anyone have any suggestions on why this is so? Thanks very much in advance.
Philip Borghesani on 20 Dec 2017
In general indexing into the output of a function call, what you call method chaining is not supported in MATLAB. Property access currently works on the output of a static function but function calls in general do not. You may find that your code behaves differently on different versions of MATLAB.
A.getB(args).print; % works because it looks like property access, not recommended
print(A.getB(args)); % will work
obj=A.getB(args); obj.print(); % or print(obj) will also work
More Answers (1)
Image Analyst on 19 Dec 2017
needs two inputs, a format string, and a variable. You only passed it the variable without passing it a format specifier string like '%f\n' or something (depends on the type of variable you want to print).
fprintf('The value = %f\n', self.Property1);