Skip to content

Tear-off equality for extensions and extension types #60065

@sgrekhov

Description

@sgrekhov

See Dart specification "17.22.3 Instance Method Closurization":

Assume that o1 and o2 are objects, m is an identifier, and c1 and c2 are function objects obtained by closurization of m on o1 respectively o2. Then c1 == c2 evaluates to true if and only if o1 and o2 is the same object.

So, we have:

class C {
  num m(int r1, {String p1 = ""}) => r1;
}

main() {
  C c = C();
  final fc1 = c.m;
  final fc2 = c.m;
  final fc3 = C().m;
  print(fc1 == fc2); // true
  print(fc1 == fc3); // false
}

That's Ok, it is according to the statement above. The same is true for mixins and enums. But for extensions and extension types we have different results.

class A {}
extension Ext on A {
  num m(int r1, {String p1 = ""}) => r1;
}

extension type ET1(int _) {
  num m(int r1, {p1 = ""}) => r1;
}

extension type ET2(int _) implements int{
  num m(int r1, {p1 = ""}) => r1;
}

main() {
  A a = A();
  final fa1 = a.m;
  final fa2 = a.m;
  final fa3 = A().m;
  print(fa1 == fa2); // false
  print(fa1 == fa3); // false

  ET1 et1 = ET1(0);
  final fet1 = et1.m;
  final fet2 = et1.m;
  final fet3 = ET1(0).m;
  print(fet1 == fet2); // false
  print(fet1 == fet3); // false

  ET2 et2 = ET2(0);
  final fet4 = et2.m;
  final fet5 = et2.m;
  final fet6 = ET2(0).m;
  print(fet4 == fet5); // false
  print(fet4 == fet6); // false
}

Why we have different results for extensions and extension types? Is there a special treatment for them?

cc @eernstg to confirm the expected result.

Dart SDK version: 3.8.0-66.0.dev (dev) (Sun Feb 2 20:05:47 2025 -0800) on "windows_x64"

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions