Giter VIP home page Giter VIP logo

Comments (5)

FMorschel avatar FMorschel commented on June 9, 2024 3

The only thing I was thinking here was for my suggestions to work the props should have mustCallSuper and this should be explained to be written like:

@override
List<Object?> get props => [...super.props, newProps];

from equatable.

FMorschel avatar FMorschel commented on June 9, 2024

This would also solve #100

from equatable.

FMorschel avatar FMorschel commented on June 9, 2024

I've tested:

Current case

All the tests on this code pass

import 'package:equatable/equatable.dart';
import 'package:test/test.dart';

class A extends Equatable {
  A(this.a);

  final int a;

  @override
  List<Object?> get props => [a];
}

class B with EquatableMixin {
  B(this.b);

  final int b;

  @override
  List<Object?> get props => [b];
}

void main() {
  test('Testing A', () {
    final a1 = A(1);
    final a2 = A(1);
    final a3 = A(2);
    expect(a1, equals(a2));
    expect(a1, isNot(a3));
    expect(a2, isNot(a3));
  });
  test('Testing B', () {
    final b1 = B(1);
    final b2 = B(1);
    final b3 = B(2);
    expect(b1, equals(b2));
    expect(b1, isNot(b3));
    expect(b2, isNot(b3));
  });
}

WARNING

But, these do as well:

//...
class A2 extends Equatable {
  A2(this.a);

  final int a;

  @override
  Type get runtimeType => A;

  @override
  List<Object?> get props => [a];
}
//...
void main() {
  //...
  test('Testing A2', () {
    final a1 = A2(1);
    final a2 = A2(2);
    final a = A(1);
    expect(a1, equals(a));
    expect(a1, isNot(a2));
    expect(a2, isNot(a));
  });
  //...
}

WARNING 2

But these all fail:

//...
enum E implements B {
  e1,
  e2;

  @override
  bool? get stringify => null;

  @override
  int get b => index;

  @override
  List<Object?> get props => [b];
}
//...
void main() {
  //...
  test('Testing E', () {
    final e1 = E.e1;
    final e2 = E.e2;
    final b1 = B(0);
    expect(e1, equals(b1));
    expect(b1, isNot(e1));
    expect(b1, isNot(e2));
  });
  //...
}
My suggested case

When the == operator is as I've configured below:
Mixin:

mixin EquatableMixin<T extends Object> on Object {
  //...
  @override
  bool operator ==(Object other) {
    return identical(this, other) ||
        other is EquatableMixin<T> && //CHANGED this line so the generic is used and removed the `runtimeType`
            equals(props, other.props);
  }
  //...
}

Equatable:

abstract class Equatable<T extends Object> {
  //...
  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Equatable<T> && //CHANGED this line so the generic is used and removed the `runtimeType`
          equals(props, other.props);
  //...
}

All the tests on this code pass

import 'package:equatable/equatable.dart';
import 'package:test/test.dart';

class A extends Equatable<A> {
  A(this.a);

  final int a;

  @override
  List<Object?> get props => [a];
}

class B with EquatableMixin<B> {
  B(this.b);

  final int b;

  @override
  List<Object?> get props => [b];
}

enum E implements B {
  e1,
  e2;

  @override
  bool? get stringify => null;

  @override
  int get b => index;

  @override
  List<Object?> get props => [b];
}

void main() {
  test('Testing A', () {
    final a1 = A(1);
    final a2 = A(1);
    final a3 = A(2);
    expect(a1, equals(a2));
    expect(a1, isNot(a3));
    expect(a2, isNot(a3));
  });
  test('Testing B', () {
    final b1 = B(1);
    final b2 = B(1);
    final b3 = B(2);
    expect(b1, equals(b2));
    expect(b1, isNot(b3));
    expect(b2, isNot(b3));
  });
  test('Testing E', () {
    final e1 = E.e1;
    final e2 = E.e2;
    final b1 = B(0);
    expect(e1, equals(b1));
    expect(b1, isNot(e1));
    expect(b1, isNot(e2));
  });
}

WARNING

But, these do as well:

//...
class A2 extends Equatable<A> {
  A2(this.a);

  final int a;

  @override
  List<Object?> get props => [a];
}
//...
void main() {
  //...
  test('Testing A2', () {
    final a1 = A2(1);
    final a2 = A2(2);
    final a = A(1);
    expect(a1, equals(a));
    expect(a1, isNot(a2));
    expect(a2, isNot(a));
  });
  //...
}

There are some possible errors in the current case (problem I presented on this issue and #100). I know it's not something everyone will do, overriding the getter of the runtimeType to any other type that has no real relation, but there is still a chance for this to happen and the equality to fail.

My current suggestion would be a breaking change if done this way, but creating a new class and a new mixin for this, could be a solution to that. And this would give the control for the equality for the user, similar to when using Comparable or some other class that uses generics. And this would keep the subclasses all equal to their base class but the other way around (like on enums cases) sometimes not.

from equatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.