Search code examples
matlaboverloadinghandlecomparison-operators

Is it possible to check for handle equality for a class that overloads eq?


My code has some handles for objects belonging to a third-party class that I can't modify. This class overloads eq so that rather than comparing whether the handles point to the same object, as the built-in version of == does, it does a different comparison based on the value of the input objects, irrespective of whether given two handles to the same object or not.

I explicitly want to check if the two handles point to the same object. I thought perhaps builtin would rescue me. But builtin('eq',A,B) just results in the error:

Error using builtin

Undefined operator '==' for input arguments of type 'ThirdPartyClass'.

It seems like builtin only likes "pure" functions, and handle.eq is a special method of the handle class that is distinct from the builtin function eq that operates on pure functions.

Calling eq explicitly with the class name using handle.eq(A,B) doesn't work either - this yields the error

No method 'eq' with matching signature found for class 'handle'.

Curiously, invoking the overload using the same syntax ThirdPartyClass.eq(A,B) yields a different error:

The class ThirdPartyClass has no Constant property or Static method named 'eq'.

So it isn't exactly clear to me if handle.eq(A,B) is also necessarily interpreted as a call to a static method. But in this exact form at least it seems not to be a viable route to calling that (overloaded) regular method.

So have I been done over by the very inconsiderate design choice of the authors of this class? Or is there some way to access the method of the superclass that has been so recklessly overloaded? Or indeed, a way to achieve what handle.eq does from scratch (despite this being one of MATLAB's built-in functions that doesn't have its implementation visible in m-code)?


Solution

  • Going over the list of public methods for handle led to one solution that is viable in this case. Since le and ge are not overloaded in this class, the expression A <= B & A >= B has the same result as A == B does for handle classes without an overload on eq.