Skip to content

Make == a symmetric method #3608

Open
@mahrud

Description

@mahrud

Equality is always a symmetric relation, and I think we should implement an exception for == so that for instance the method Vector == ZZ will automatically also be used if ZZ == Vector is called.

Here is a potential implementation:

eq0 = lookup(symbol==, Thing, Thing)
Thing == Thing := (a,b) -> (
    if lookup(symbol==, class b, class a)
    =!= lookup(symbol==, class a, class b)
    then b == a else eq0(a,b))

I don't like it very much, but it works:

i1 : eq0 = lookup(symbol==, Thing, Thing);

i2 : Thing == Thing := (a,b) -> (
    if lookup(symbol==, class b, class a)
    =!= lookup(symbol==, class a, class b)
    then b == a else eq0(a,b));

i3 : 1 == vector {1,2}
stdio:2:149:(3):[1]: error: no method for binary operator == applied to objects:
            1 (of class ZZ)
                              2
     ==     | 1 | (of class ZZ )
            | 2 |

i4 : Vector == ZZ := (v,f) -> matrix v == f;

i5 : 1 == vector {1,2}
stdio:4:34:(3):[2]: error: matrices have different shapes

i6 : 1 == vector {1}

o6 = true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions