.equals() on Arrays not working

Hi,

I am saving an array as a Property. When I check the value when loaded, the .equals() call to the same array contents is returning false. However if I convert both into strings I get true.

As anybody else had trouble with .equals() on array data types?

Thanks
Chris
  • I've never had the need to compare arrays, but the problem is most likely related to the definition of equality for arrays. Given that everything in MonkeyC is a reference, there are different definitions of `equal' to consider.

    The first is object identity.

    // a and b are references to the same underlying array...
    // [ a ]-+->[ ]-->[ 1 ]
    // | [ ]-->[ 3 ]
    // [ b ]-+ [ ]-->[ 4 ]
    //
    var a = [ 1, 3, 4 ];
    var b = a;
    assert (a.equals(b));


    The second is value equality. I'm showing two different cases, but the idea is the same.

    // a and c are references to unique unique underlying arrays, but
    // the elements in those arrays are the same element instances...
    // [ a ]-->[ ]-->[ 1 ]<--[ ]<--[ c ]
    // [ ]-->[ 3 ]<--[ ]
    // [ ]-->[ 4 ]<--[ ]
    var c = new [ 3 ];
    for (var i = 0; i < 3; ++i) {
    c= a;
    }
    assert (a.equals(c));
    [/code]

    // a and d are references to unique underlying arrays, and the elements
    // in those arrays are unique instance (unless the VM implements object interning)...
    // [ a ]-->[ ]-->[ 1 ]
    // [ ]-->[ 3 ]
    // [ ]-->[ 4 ]
    //
    // [ d ]-->[ ]-->[ 1 ]
    // [ ]-->[ 3 ]
    // [ ]-->[ 4 ]
    var d = [ 1, 3, 4 ];
    assert (a.equals(d));


    It seems that the definition of equals only considers identity by default and must be overridden to get the correct class-specific behavior (arrays and dictionary should be verified). In languages like Java the == comparison is used to see if two references refer to the same object (identity) and equals is used to determine if the objects are equal (value equality). It seems you want value equality, and I agree that this is what you should get.

    Travis
  • Thanks Travis, yes I was after Value equality, thus using .equals() over ==

    As proved, and the code work around I have of converting to strings, .equals() works for basic types but not Arrays. I haven't tried Dictionaries, and am also using 1.2.9

    Therefore it looks like a bug, or not implemented/overridden for Arrays

    Cheers
    Chris
  • I'll get this reported--I'm not certain what the intended behavior is here.
  • Thanks Brandon.

    The intended behavior, is that if you have two separate arrays which contain the same number of values, with the same contents (type and value) at each of the locations, they should be classified as equal.

    Thanks
    Chris
  • Right, I understand your intent, but I'm not sure what we intend. :) I agree that .equals() should do value equality, but there may be some reason I am not aware of that explains why it does not right now.
  • Ok

    The help states:
    Test if this instance is equal to another instance of an object by using equals().

    Parameters:
    that — That item to test against
    Returns:
    (Boolean) — true if objects are equal, false otherwise.


    I would expect it to check value equality, as reference equality should be with ==