Avoid double validation for constructor arguments that are class properties?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Matt J
el 7 de Ag. de 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 comentarios
Matt J
el 7 de Ag. de 2023
Editada: Matt J
el 7 de Ag. de 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.
Ver también
Categorías
Más información sobre Argument Definitions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!