Why does tab completion not work for certain subclass methods?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 1 Nov 2023
Réponse apportée : MathWorks Support Team
le 13 Déc 2023
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?
Réponse acceptée
MathWorks Support Team
le 1 Nov 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.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Class Introspection and Metadata dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!