Avoid double validation for constructor arguments that are class properties?
21 views (last 30 days)
Show older comments
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:
- Is it possible to achieve similar behavior as in my first example without copying the validation functions?
- Is there a way to auto complete one=..., i.e. using the new argument assignment schema myclass(one=123) instead of myclass("one",123)?
0 Comments
Answers (1)
Matt J
on 7 Aug 2023
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 Comments
Matt J
on 7 Aug 2023
Edited: Matt J
on 7 Aug 2023
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.
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!