Code completion in editor for function return values?
2 ビュー (過去 30 日間)
古いコメントを表示
If you have a class, say "Car", and you directly create an object of it like below you get code completion in the editor on methods and properties. Like below it would show "change_gear" after the initial part of the method is types.
my_car = Car();
my_car.change_g.. % <- code completion
But if I have a method that returns an object as an output argument then code completion does not work. Adding an "arguments (Output)" to validate the returned argument as a specific type does not help either. Am I missing some way to make it work?
It is very unfortunate that is does not work, because it is a very common pattern that a function or method returns an object.
function car = create_car()
arguments (Output)
car Car
end
car = Car();
end
my_car = create_car();
my_car.change_ge.. % <- No code completion.
回答 (1 件)
Walter Roberson
2023 年 10 月 6 日
This is not going to be possible in many cases. The editor would need to evaluate create_car in order to determine the data type of my_car .
I agree that having the arguments (Output) block does give a substantial hint, but I would ask about a couple of cases:
Case 1:
my_car = create_car('ford');
my_car.change_ge..
create_car does not accept any input parameters, so should the editor be smart enough to understand that the call to create_car should error and so my_car will not be class Car ?
Case 2:
function car = create_car()
if rand() < 0.5
car = Car();
else
car = Bread();
end
end
what should the editor deduce about the properties of my_car ?
2 件のコメント
Walter Roberson
2023 年 11 月 10 日
The type analysis that is done is restricted to the same scope, not to objects returned from a function.
It appears to me that (somewhere) it has some information about the class returned by some of the common Mathworks-provided functions, and that it runs off that information. Plausibly it might also be able to handle objects created by a call to the constructor of a user-provided class.
Oh wait, it's worse than that!
function testtest2
a = plot([], []);
b = plot([], []);
c = plot([], []);
d = plot([], []);
e = plot([], []);
f = plot([], []);
g = plot([], []);
h = plot([], []);
i = plot([], []);
j = plot([], []);
k = plot([], []);
l = plot([], []);
m = plot([], []);
n = plot([], []);
o = plot([], []);
p = plot([], []);
r = plot([], []);
s = plot([], []);
t = plot([], []);
u = plot([], []);
v = plot([], []);
w = plot([], []);
x = plot([], []);
y = plot([], []);
z = plot([], []);
a.
end
Position to the a. and wait a second. No completions wil come up. The same is true for b., c., d. -- but e., f., g. will all give suggestions of XData and others... but then several more letters will not.
In my test it was repeatable that the letters it was able to give suggestions for were:
e, f, g, l, m, o, p, r, s, t, u, v, w, z
But not any of the other single-character lower-case variables.
This is something I just cannot explain -- if it is doing flow analysis then surely all of the variables should be tracked the same way, with it not being expected that you would fail for random variable names. You might maybe guess that i and j would not work because of their meanings as functions (but flow control is the working hypothesis so why would that matter), but the ones it works for or not is more or less random
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!