Skip to content

Optimize operator (==) overloads when rhs or lhs is null #1891

Open
@saulecabrera

Description

@saulecabrera

Considering a class that defines operator overloads, especially those around equality, would it make sense to optimize the case where the rhs or the lhs or both are null?

class Foo {
  @operator("==")
  static __op(lhs: Foo, rhs: Foo) {
    // define equality
  }

I know that internally this desugars to a normal static function call and thus the overload is probably responsible for handling the null case. Even though this is true, from a proper equality comparison in the case of classes, this behaviour seems counterintuitive given the equality should be performed based on the object properties and not necessarily on its type (T | null).

This case is especially noticeable when performing a comparison between instances of classes that have nested properties that are instances of classes defining overloads, i.e.

class A {
   b: B | null;
   // defines == overload
}


class B {
  // defines == overload
}

const a: A = {b: null};
const a1: A = {b: new B()};

Comparing the two instances above will fail, because the equality overload defined in B doesn't expect null.

All this to say: given that the overload is defined on a per-class basis, would it make sense to only invoke the overload if the value is not null and return false otherwise? I agree that having the overload be invoked with null is probably the most flexible option, but at least with classes I doubt that there's a valid case in which a class instance will compare itself to null and be true

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions