Avoid double validation for constructor arguments that are class properties?

15 次查看(过去 30 天)
I understand that class properties can be validated via property validation and I love it. I even love more that there's an option to use the meta class to auto-complete and auto-validate class properties like this:
classdef myclass
properties
one
two string {mustBeMember(two, ["only two options are allowed", "option 2"])}
end
methods
function obj = myclass(opts)
arguments
opts.?myclass
end
% assign the properties to the object here
end
end
end
It results in this, which is beyond awesome, because it auto-completes the properties, but even also shows allowed members of the property. I.e., there's no additional function arguments check required anymore.
But now, assuming that there are not only optional, but one required property like this:
classdef myclass
properties
one
two string {mustBeMember(two, ["only two options are allowed", "option 2"])}
end
methods
function obj = myclass(requiredPropTwo, opts)
arguments
requiredPropTwo
opts.?myclass
end
obj.two = requiredPropTwo;
% assign the properties to the object here
end
end
end
Now, there's no way to auto-complete property two's options anymore:
Of course, assigning the property fails, but Matlab does not understand that there's a direct linkage between my constructor argument the class property.
The only way to get proper auto-completion would be to copy the validation from the property - now we have it doubled which is not a good practice.
function obj = myclass(requiredPropTwo, opts)
arguments
requiredPropTwo string {mustBeMember(requiredPropTwo, ["only two options are allowed", "option 2"])}
opts.?myclass
end
obj.two = requiredPropTwo;
% assign the properties to the object here
end
Questions:
  1. Is it possible to achieve similar behavior as in my first example without copying the validation functions?
  2. Is there a way to auto complete one=..., i.e. using the new argument assignment schema myclass(one=123) instead of myclass("one",123)?

回答(1 个)

Matt J
Matt J 2023-8-7
Is it possible to achieve similar behavior as in my first example without copying the validation functions?
No.
Is there a way to auto complete one=..., i.e. using the new argument assignment schema myclass(one=123) instead of myclass("one",123)?
It seems to be there already:
  2 个评论
Jan Kappen
Jan Kappen 2023-8-7
编辑:Jan Kappen 2023-8-7
Okay, that means as long as I want to set non-optional class properties via constructor (or other methods) arguments, and want to keep the class properties public settable, I have to duplicate the validation code, is that correct?
> It seems to be there already:
Right.. but not for the first optional name-value-argument, the default auto completion is this:
I can now either push <tab>, and we're in the legacy style of setting parameters, or I can type "one=" which then gets understood by Matlab. The second argument indeed is proposed using the new syntax.
I'd like to just disable auto-completions using the old syntax at all. I'm using the new desktop experiene if that makes a difference.
Thanks!
Edit: I assume, there's no way to re-use function argument validation in function/method calls, too? Described here: Function argument validation and editor auto-completion in wrapper functions/classes - MATLAB Answers - MATLAB Central (mathworks.com)
Matt J
Matt J 2023-8-7
编辑:Matt J 2023-8-7
Okay, that means as long as I want to set non-optional class properties via constructor (or other methods) arguments, and want to keep the class properties public settable, I have to duplicate the validation code, is that correct?
Well, only if you want the autocomplete functionality. If all you care about is validation, then this line already intercepts bad input values because of the property validation code,
obj.two = requiredPropTwo;
I assume, there's no way to re-use function argument validation in function/method calls, too?
It would be nice, but I don't think there is. You can write your own customized validation function which can be reused wherever you like, but I don't think Matlab knows how to generate autocompletions for user-defined validation functions, which I think is what you really want.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by