Why does tab completion not work for certain subclass methods?

4 次查看(过去 30 天)
I am trying to use tab completion with methods of a subclass, but it is not showing tab completion results for these methods.
I have defined a class, "ClassA" in MATLAB. This class has a method "printX" and property, "x": 
classdef ClassA
properties
x
end
methods
%% Constructor
function obj = ClassA(x)
obj.x = x;
end
%% Helper Function
function printX(obj)
arguments
obj ClassA
end
disp(obj.x)
end
end
end
I also defined a class "ClassB" that inherits from "ClassA":
classdef ClassB < ClassA
methods
function obj = ClassB(x)
% Create the object
obj = obj@ClassA(x);
end
function Print(obj)
arguments
obj ClassA
end
% Print the object property
obj.printX();
end
end
end
I used argument validation blocks in the subclass method, "Print", to validate the object, "obj", as an instance of the superclass.
While methods defined in the superclass show up with tab completion, the methods defined in the subclass do not show up in the MATLAB Command Window or MATLAB Editor. However, they appear in the output list when using the "methods" function, and they work correctly when typing the method without tab completion.
After creating an instance "MyInstance" of the subclass, tab completion does not show the "Print" method when typing "MyInstance. + <TAB>".
Why does tab completion not work for certain subclass methods?

采纳的回答

MathWorks Support Team
This is expected behavior and can be worked around by declaring a subclass object instead of a superclass object in the argument validation block as shown below.
function Print(obj)
arguments
obj ClassB;
end
% Print the object property
obj.printX();
end
To explain, this occurs because the method belongs to the subclass while the "argument validation" block specifies an instance of the superclass. Therefore, tab completion is not aware of the method since the method does not belong to the superclass. MATLAB is dynamically-typed, so it is aware of the method during execution, which explains why it is listed in the output of the "methods" function and why the method itself works. In contrast, tab completion does not have access to that runtime information, so it cannot provide the method in the tab completion results.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Class Introspection and Metadata 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by