Customize isequal behavior for handle objects?

조회 수: 18 (최근 30일)
Thomas Kiefer
Thomas Kiefer 2020년 1월 2일
댓글: Thomas Kiefer 2020년 1월 3일
Is it possible to customize the isequal function for own classes, i.e. to define when two handle-class objects are considered as equal by content? I would like to explain my question in more detail below:
Please consider the following simple data class
classdef Data < handle
properties
value
end
methods
function self = Data(value)
self.value = value;
end
end
end
Following MathWorks --- Equality of Handle Objects one can check if two objects of the Data class point to the same reference using the == operator. In contrast, one can test if two objects are equal by content using the Matlab built-in isequal function.
This works fine. However, I now would like to override the isequal behaviour for my class. For example, I would like to define that two Data objects are equal by content if the values of their property value are equal within an absolute tolerance of 1e-8. How could I achieve that?
Note:
Im aware of the possibility of overriding the eq-method of the Data class:
classdef Data < handle
properties
value
end
methods
function self = Data(value)
self.value = value;
end
function is_eq = eq(self, other)
if not(strcmp(class(self), class(other)))
is_eq = false;
return
end
is_eq = abs(self.value - other.value) < 1e-8;
end
end
end
Therewith I can test the equality of two objects by content using the == operator. However, from my point of view this is problematic because: i) It is inconsistent with the initial/previous behavior because the == operator no longer compares two objects by reference but by content instead. And, ii) in addition, I lose the possiblity to check if two objects have the same reference.

채택된 답변

Steven Lord
Steven Lord 2020년 1월 2일
If you want to overload what isequal means for your object, overload isequal.
isequal and eq are somewhat related, but they are independent functions.
  댓글 수: 1
Thomas Kiefer
Thomas Kiefer 2020년 1월 2일
I wasn't aware about the fact isequal could be overload for own classes. Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Thomas Kiefer
Thomas Kiefer 2020년 1월 3일
One more subsequent question: When overriding isequal in a custom class, i.e. in the Data class, how could one access the previous definition of isequal (as if I would not have overriden the method)? Because isequal is not a member method of the base class handle.
I.e., what I would like to do in my custom isequal method is to make some quick checks on the objects by which I can verfiy that the objects are different and only if these checks are passed use the full isequal-test. Like that:
classdef Data < handle
properties
value
end
methods
function self = Data(value)
self.value = value;
end
function is_eq = isequal(self, other)
%% Make some user specific tests
%% Which help to quickly decide if self and
% other are unequal
%...
%...
%% If all criteria above are fulfilled use the
%% Matlab default isequal... like isequal@superclass(self, other)
is_equal = isequal(self, other);
end
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 1월 3일
builtin('isequal', self, other)
Thomas Kiefer
Thomas Kiefer 2020년 1월 3일
Great! That solves my problem. Thank you very much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by