Code completion in editor for function return values?

조회 수: 2 (최근 30일)
Jim Svensson
Jim Svensson 2023년 9월 12일
댓글: Walter Roberson 2023년 11월 10일
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
Jim Svensson
Jim Svensson 2023년 11월 10일
편집: Jim Svensson 2023년 11월 10일
Somehow it is possible to do it, at least for internal Matlab functions.
ThemeCopy
p = buildplan();
p. % <- this will give code completion on the class matlab.buildtool.Plan
% returned by function buildplan()

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
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
Jim Svensson
Jim Svensson 2023년 11월 10일
Yes Matlab is a dynamically typed language so it is of course imposssible to know the type of variables in many cases. But Matlab is already doing some one the fly type analysis and all I want is that it support some reasonable simple cases.
Walter Roberson
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by