Why does tab completion not work for certain subclass methods?

4 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 1 de Nov. de 2023
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.

Más respuestas (0)

Categorías

Más información sobre Class Introspection and Metadata en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by