Avoid double validation for constructor arguments that are class properties?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Matt J
am 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 Kommentare
Matt J
am 7 Aug. 2023
Bearbeitet: Matt J
am 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.
Siehe auch
Kategorien
Mehr zu Argument Definitions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!