Dynamic properties auto completion in argument validation
조회 수: 23 (최근 30일)
이전 댓글 표시
I love argument validation, and especially the option to use the ? operator to automatically list all class properties.
However, I'm experimenting with dynamic properties which (obviously) can't be auto completed, compare this class:
classdef testclass < dynamicprops
properties
prop1 string = "prop1"
end
methods
function obj = testclass()
obj.addprop("dynamicProp1");
end
function autoComplete(obj, props)
arguments
obj
props.?testclass
end
disp("this auto completes only 'prop1'")
end
end
end
Calling the class via
t = testclass
t.autoComplete(<tab>)
shows "prop1" as expected.
Is there any way to add dynamic properties or any other non-class-constant options? Can the props. in the argument validation section be filled with strings at runtime, like props.createStruct(), where createStruct() is some arbitrary function?
I think the key issue is that this happens at runtime, not at "compile"/definition time, is that right?
Are there workarounds like
function autoComplete(obj, props)
arguments(Repeating)
props {mustBeMember(props, [???])}
end
end
? The argument in mustBeMember seems have to be also known at definition time.
Any ideas?
Thanks, Jan
댓글 수: 0
답변 (1개)
Steven Lord
2024년 3월 6일
The testclass class doesn't have a property named dynamicProp1, and the documentation for the syntax you're using states "A useful function syntax in MATLAB uses the public properties of a class for the names of name-value arguments." (See the Name-Value Arguments from Class Properties section.)
The specific instance of the testclass class that you're passing into autoComplete has a property named dynamicProp1. But the metaclass syntax (the ?nameOfClass syntax) doesn't operate on instances (actually the metaclass function can accept an instance, but it returns the meta.class object for the class not a meta.instance object for that specific instance. There is no "meta.instance" equivalent.)
You haven't shown how you want your autoComplete method to use the data from props, but you could define your own local validation method or function (see the Define Validation Functions section) to allow autoComplete to accept any property name of the instance as the props input (though it won't play with automatic completion if you do.) A very quick version of that could be:
classdef testclass < dynamicprops
properties
prop1 string = "prop1"
end
methods
function obj = testclass()
obj.addprop("dynamicProp1");
end
function autoComplete(obj, props)
arguments
obj
end
arguments(Repeating)
props {mustBePropertyOfClassOrNumber(obj, props)}
end
% Add additional checking (even number of entries in props,
% coming in name-value pairs) here
%
% That could be as simple as setting the properties:
for whichArg = 1:2:numel(props)
fprintf("Setting property %s.\n", props{whichArg})
obj.(props{whichArg}) = props{whichArg+1};
end
disp("this auto completes only 'prop1'")
end
end
methods(Access=private)
function mustBePropertyOfClassOrNumber(obj, props)
if isnumeric(props) % value
return
else % name
assert(isprop(obj, props), ...
"Property " + props + " is not a property of the object.")
end
end
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!