-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typed equality operator #4176
Comments
Would a class be able to implement the Since class A {
// many fields
bool operator ==(Object other) {
if (other is! A) return false;
return this === other;
}
bool operator ===(A other) {
// compare many fields
}
} If anyone copy/pastes I don't know if I'd expect this to be a problem in practice and the anti-patterns are probably detectible in a lint. I do think that the way these operators are layered is opposite to what is intuitive given the types and that make me a little nervous. |
Another option is to allow That can actually be seen as a generalization of the current It'll be a runtime type check, but if every A non-up-cast invocation would be statically checked against the actual declared parameter type. (And that parameter type can be useful as a context type.) |
Not sure I buy the I don't see anybody actually switching to it, or some might, but most won't, and then we have two ways to do things, one that's too lenient and one that's too strict. It doesn't work for, fx, numbers, or sealed classes, where you may want to compare to something of the supertype I think an bool eq<T>(T v1, T v2) => v1==v2; could work as the I don't see myself using that either, ever. |
I like the idea of improving |
Declaring Changing the implementations could be breaking if it throws, but if they can be changed to |
There has been a long-standing tension in Dart between the desire to have
operator==
be typed tightly to take the type of the receiver, and to have it typed loosely to avoid runtime errors when callingoperator==
on a receiver whose static type is more general than the runtime type. Examples:The above program, when run with asserts enabled, prints "Oopsie". Making the type of the argument to
operator==
onB
be covariant gives nice static typing behavior, but interacts poorly with subsumption.The current solution to this is to use the "unrelated_type_equality_checks" lint which is included in the core set of lints recommended by the Dart team.
Recently, @eernstg suggested adding a typed variant of equality:
e1 =<T>= e2
which gives a static error if eithere1
ore2
are not a subtype ofT
, and then treating the standarde1 == e2
syntax as syntactic sugar fore1 =<Object?> e2
. It occurred to me that we could essentially get the same functionality using an extension method if we added a new equality operator. That is, if we added something like===
(to pick a random example), then we could also add to the core libraries the following extension:Statically, this enforces that the argument has a type which is a subtype of the type of the receiver, without changing anything about the runtime behavior (essentially encoding the above mentioned lint into the type system). Unlike the lint above, this is an actual static type, and so it can interact with inference and other language mechanisms (e.g. #4149).
@dart-lang/language-team thoughts?
The text was updated successfully, but these errors were encountered: