Comments (5)
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.
This would also solve #100
from equatable.
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 enum
s cases) sometimes not.
from equatable.
Related Issues (20)
- Same hashcode for different new lists HOT 1
- Equality and Hashcode differ for Sets with different order
- Equatable doesn't checks for Equality for variables in parent class HOT 3
- feat request: add `stringifyProps` HOT 4
- Same object, same runtimeType but runtimeType== other.runtimeType is false HOT 3
- Storing the hashCode HOT 3
- Doesn't work with List<File> as property HOT 1
- `runtimeType` prevents from using generic type HOT 5
- Provide lint for forgotten fields in props HOT 3
- List of object dosen't work correctly HOT 12
- `Equatable` is redundant with `EquatableMixin` HOT 2
- Upgrade sdk to include Dart 3 HOT 3
- A Map considered equal even if its not HOT 3
- Why do we need to compare runtimeType? HOT 5
- Support for mutable class objects HOT 3
- `Foo(1) == Foo(1.0)` is false HOT 2
- Do Equatable actually need to be immutable HOT 1
- Publish a new version HOT 2
- Is that logical? Object equals but element not equals. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from equatable.