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:
- 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
답변 (1개)
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
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 Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!