Tab completion behavior is different for constructors

39 views (last 30 days)
Jaime
Jaime on 30 Dec 2024 at 14:04
Commented: Jaime on 31 Dec 2024 at 17:10
I've been working on developing classes in Matlab (I'm fairly new at using Matlab for OOP) and noticed some odd and minorly annoying behavior. If I write a method ("func") for a class ("class1") and use and argument validation block with a mustBeMember() validation function for an argument ("arg1"), I get the tab completion behavior I would expect when calling that function. What seems odd to me is that I don't get this behavior when calling a constructor that uses property validation in the exact same manner. See the sample code below which defines a class with a constructor and method that show this behavior.
classdef class1
properties
prop1 (1,1) string {mustBeMember(prop1,["aa" "bb"])} = "aa"
prop2 string
end
methods
function c = class1(prop1) % Tab completion doesn't work for prop1 when calling constructor
% If there was an arguments block here with mustBeMember
% validation, tab completion would work... seems duplicative
c.prop1 = prop1;
end
function c = func(c,arg1) % Tab completion works for arg1 when calling func
arguments
c class1
arg1 (1,1) string {mustBeMember(arg1,["cc" "dd"])} = "cc"
end
c.prop2 = arg1;
end
end
end
Am I doing something wrong? Is this not a normal behavior to expect? If I want this behavior, I have to add an arguments block to the constructor... which seems duplicative of the property validation that I am already doing. When writing custom classes where properties are resitricted to a list of values, it would be nice for the user to see those possible values when they tab over, thus making working with the class more inuitive.
Thoughts?

Answers (1)

Abhas
Abhas on 30 Dec 2024 at 14:45
Hi @Jaime,
The tab completion works differently in the following two cases:
  • Method Argument Validation (arguments block): When you use an "arguments" block with a validation function like "mustBeMember" in a method, MATLAB explicitly associates that validation with the method's input arguments. Tab completion works here because MATLAB is aware of the specific context and the list of valid values for that argument.
  • Property Validation in the Constructor: Property validation (e.g., {mustBeMember(prop1,["aa" "bb"])}) applies when properties of an object are being set or assigned. It doesn’t directly affect tab completion during the constructor call as MATLAB does not directly associate property validation with the constructor arguments. The constructor’s input argument (prop1) is just a generic variable at this stage, and the validation is applied only when you assign "prop1" to the property.
You may refer to the below documentation link to know more about tab completion: https://www.mathworks.com/help/imaq/using-tab-completion-for-functions.html
I hope this helps!
  1 Comment
Jaime
Jaime on 31 Dec 2024 at 17:10
@Abhas, thanks for your response!
If I understand you correctly, you're affirming that that the behavior I observe is nominal -- there is no tab completion for class constructors where the class properties have validation functions. Agreed?
Two follow-up questions for you or anyeone else:
  1. Would it be common practice to duplicate property validation in an arguments block in a constructor, in order to get tab completion behavior? This feels wrong to me.
  2. How is it that in-build MATLAB objects seem to support this behavior? When I call figure(), for example, tab completion works.

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!