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:
classdef A
methods (Static)
function b = getB(args)
b = B(args);
end
end
end
And then I want to use class B to do something e.g. use the print() method, where class B is:
classdef B
properties
Property1
end
methods
function self = B(args)
self.Property1 = args;
end
function print(self)
fprintf(self.Property1);
end
end
end
Trying
A.getB(args).Property1
succeeds.
I would thus also expect
A.getB(args).print()
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
Philip Borghesani 2017 年 12 月 20 日

1 投票

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.
Try
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

5 件のコメント

Benjamin Chen
Benjamin Chen 2017 年 12 月 20 日
Thanks for this explanation Philip. I have noticed that when I make getB a non-static method and then call it from an instance of A, i.e:
a = A()
a.getB(args).print()
This works. Could you also give some insights into why this is so?
Philip Borghesani
Philip Borghesani 2017 年 12 月 20 日
It's not static vs nonstatic method that makes the difference it is static call vs call on an object. Even if getB is static then:
a=A();
a.getB(args).print() % or
A().getB(args).print() %works
a=A();a.getB is simpler to parse then A.getB because from code analysis it is obvious that a is a variable so getB(args) must be a property access or method call but A.getB(args) could also be a call to the static method getB in the package A.
Benjamin Chen
Benjamin Chen 2017 年 12 月 20 日
That sounds reasonable, thanks. I wish the error message could have been this informative rather than just "Not enough input arguments".
Philip Borghesani
Philip Borghesani 2017 年 12 月 20 日
I agree, the error message should have been better.
This appears to be fixed so that a static call works correctly in a future version of MATLAB.
Benjamin Chen
Benjamin Chen 2017 年 12 月 20 日
That's great news, looking forward to it!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 12 月 19 日

0 投票

fprintf(self.Property1);
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);

1 件のコメント

Benjamin Chen
Benjamin Chen 2017 年 12 月 19 日
Thanks for the answer Image Analyst. It seems the error is not at the fprintf part, but in the static getB method of class A.
Even if I replace the fprintf with disp, it still gives the same error.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Get Started with MATLAB についてさらに検索

製品

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by