-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Less boilerplate for operator == #36004
Comments
I understand that the focus is on equality here, but promotion seems to be an important topic as well. For that, note that #25565 (comment) lists a number of other issues targeting promotion improvements, and #25565 is in general a good hub for keeping discussions on that topic together. In the language team we have discussed using an expression statement like The last example using However, the test |
We could also just ban overrides of |
Can we update the first post from "Our current best-practice" to "Flutter's current best-practice"? Using I do think it would be worthwhile to explore disallowing overrides of |
Going further, Flutter SDK's best practice. Most Flutter users do not use this pattern. |
Would it not be valid to write the following? Short-circuiting at the type-check seems to protect against illegal accesses. @override
bool operator ==(other) =>
other is ThisClass
&& other.bar == bar
&& other.baz == baz
&& other.quux == quux; As a Swift developer, I love that the Swift compiler synthesizes |
Using |
Was my example asymmetric? I think this is the kind of code most people will want to write, now I'm doubting it. :) |
consider class ThatClass extends ThisClass {
ThatClass(this.bob, Bar bar, Baz baz, Quux quux) : super(bar, baz, quux);
final Bob bob;
operator ==(other) =>
other is ThatClass
&& other.bob == bob
&& super==(other);
}
// ...
print(ThatClass(a, b, c, d) == ThisClass(b, c, d)); // false, good
print(ThisClass(b, c, d) == ThatClass(a, b, c, d)); // true, oops |
> Using `is` in `operator ==` is usually wrong as it makes the `==` operator asymmetric. dart-lang/sdk#36004 (comment)
> Using `is` in `operator ==` is usually wrong as it makes the `==` operator asymmetric. dart-lang/sdk#36004 (comment)
> Using `is` in `operator ==` is usually wrong as it makes the `==` operator asymmetric. #36004 (comment)
Our current best-practice for
operator ==
is:It's unfortunate that we have to introduce a new variable here. It would be nice if this worked:
...or maybe with new syntax:
...or even:
See also:
The text was updated successfully, but these errors were encountered: