bugGNU Octave - Bugs: bug #53911, default implementation of 'eq' for...

 
 

bug #53911: default implementation of 'eq' for handle classes

Submitter:  Richard <crobar>
Submitted:  Tue 15 May 2018 02:13:18 PM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Need Info Assigned to:  None
Originator Name:  Richard Crozier Open/Closed:  * Open
Release:  * 4.4.0 Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 06 Apr 2022 10:09:10 PM UTC, comment #4: 

Came across the same issue for detecting copies of handle class objects. It is documented in Matlab that a 'eq' method is automatically defined for handle classes:

https://www.mathworks.com/help/matlab/ref/handle.relationaloperators.html
"Copies of a handle variable always compare as equal."

Guillaume <gyom>
Tue 17 Mar 2020 09:21:07 PM UTC, comment #3: 

This bug is marked 'Need Info' is this actually still the case? I provided additional info.

Richard <crobar>
Tue 15 May 2018 06:18:47 PM UTC, comment #2: 

True, but I also need some way of uniquely identifying the instance, like adding a random number property or something like this. I can probably come up with something, but if there's some other method of uniquely identifying objects that would be useful.

The default behaviour of isequal and == for handle classes is mentioned briefly in this doc:

https://uk.mathworks.com/help/matlab/ref/handle.relationaloperators.html

A more comprehensive discussion is here:

https://uk.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html

basically handle class has a default eq operator. == returns true if the instances are the same, isequal returns true if the property values are the same.

For value classes there is not a default eq operator, but isequal will return true if the objects are of the same class and have the same property values.

There does seem to be a default ne operator for handle classes too:


>> y = x

y =

  compare1 with properties:

    foo: 1.0000e+000

>> x ~= y

ans =

  logical

   0


Richard <crobar>
Tue 15 May 2018 05:18:44 PM UTC, comment #1: 

Well you can add your own 'eq' overload method to your class to do a value comparison between two instances.

So Matlab has a default 'eq' operation for classdef instances? I'm guessing also a 'ne' operation? What about value classes? What are the default equality semantics for handle or value classes? Are these default operations documented anywhere?

Mike Miller <mtmiller>
Group Member
Tue 15 May 2018 02:13:18 PM UTC, original submission:  

In matlab you can compare two handle class object to determine if the are actually the same instance, e.g. in Matlab:


>> addpath ('/home/rcrozier/src/mfiles/mcode-hg/octave_test/test_comparing_handle_classes_using_eq')
>> x = compare1 ()

x =

  compare1 with properties:

    foo: 1.0000e+000

>> y = x

y =

  compare1 with properties:

    foo: 1.0000e+000

>> z = compare1

z =

  compare1 with properties:

    foo: 1.0000e+000

>> x == y

ans =

  logical

   1

>> x == z

ans =

  logical

   0


In Octave this is not possible:


>> x = compare1 ()
x =

<object compare1>

>> y = x
y =

<object compare1>

>> z = compare1
z =

<object compare1>

>> x == y
error: eq method not defined for compare1 class
>>


Is there any workaround to determine if two handle objects are actually the same?

Richard <crobar>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #44161:  compare1.m added by crobar (98B - text/x-objcsrc)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by crobar (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-03-27 mtmiller Carbon-CopyRemoved 80942 -
    2018-05-15 mtmiller CategoryNone Interpreter
        StatusNone Need Info
        SummaryComparing handle classes using == fails default implementation of 'eq' for handle classes
    2018-05-15 crobar Attached File- Added compare1.m, #44161

    Back to the top

    Powered by Savane 3.13-cf05.
    Corresponding source code