y = 
Argument validation for class methods
显示 更早的评论
Is there a best practice for validating (or not validating) the 'self' arguments in class method definitions?
Consider:
classdef my_class < handle
properties
A;
end
methods
function obj = my_class
obj.A = 0;
end
function add_to_obj(obj, B)
arguments
obj my_class; %This seems unnecessary
% obj; %Alternately we could omit the 'class' specifier
B (1,1) double;
end
obj.A = obj.A + B;
end
end
end
The class specification of the obj argument seems superflous and should be self-evidently true. Are there performance implications for leaving it in there (i.e., if I'm calling add_to_obj a lot)? Should I just leave out the specifier (essentially bypassing the argument validation)?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
