Skip to content

Conversation

@tarrinneal
Copy link
Contributor

adds == and hash methods to classes

@tarrinneal tarrinneal marked this pull request as ready for review April 2, 2025 19:02
return true

case let (cleanLhsHashable, cleanRhsHashable) as (AnyHashable, AnyHashable):
return cleanLhsHashable == cleanRhsHashable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashable doesnt mean equal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashable inherits from equitable, there is no AnyEquitable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i misread. i thought you were comparing 2 hash values.

}
}
return true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also check the case when both conform to Equatable, then use == to compare them? (e.g. String is Equatable)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String is Hashable, therefore already covered. Any type that is supported by pigeon is covered by hashable except lists and maps. And certain classes in proxy apis, but this isn't meant to solve for non-Hashable classes, as there is no real way to compare if they aren't Equitable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry a String isn't a good example. Worth putting a comment there to explain a bit, since this is missing Non Hashable but Equatable case.

return true

default:
return String(describing: cleanLhs) == String(describing: cleanRhs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does the input come from? one could overwrite a type's description to return arbitrary strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I couldn't find a better way to manage this besides just returning false. This seemed potentially better? This isn't (theoretically) hittable anyway since pigeon types are limited

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when will we hit the default case? we might as well just return false if we already covered all known cases that return true. Also worth adding a comment.

description == other.description &&
code == other.code &&
deepEqualsMessages(data, other.data)
return deepEqualsMessages(toList(), other.toList())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW this is making the generated code shorter at the cost of making the method less efficient, since now it has to create two new arrays.

I don't think the efficiency matters much, but worth noting that there is a tradeoff, it's not just shorter.

case let (cleanLhsDictionary, cleanRhsDictionary) as ([AnyHashable: Any?], [AnyHashable: Any?]):
guard cleanLhsDictionary.count == cleanRhsDictionary.count else { return false }
for (key, cleanLhsValue) in cleanLhsDictionary {
guard let cleanRhsValue = cleanRhsDictionary[key] else { return false }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this give the wrong answer if two equal dictionaries have a nil value for some key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't support nil keys

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not nil keys, nil values. guard let cleanRhsValue = cleanRhsDictionary[key] else { return false } will unconditionally return false for "foo": nil even if both dictionaries have it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, misread, my bad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no solution to this problem that doesn't include creating a list of keys

}

if let valueDict = value as? [AnyHashable: AnyHashable] {
for key in valueDict.keys {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (key, value) in valueDict {

As with the Kotlin comment: if you need keys and values, an iterator that keeps both is more efficient than iterating keys and then looking up the value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already in this shape, not sure why it's not showing in your review

Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tarrinneal tarrinneal added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 8, 2025
@auto-submit auto-submit bot merged commit 2405f6a into flutter:main Apr 8, 2025
82 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 8, 2025
androidseb pushed a commit to androidseb/packages that referenced this pull request Jun 8, 2025
adds == and hash methods to classes
FMorschel pushed a commit to FMorschel/packages that referenced this pull request Jun 9, 2025
adds == and hash methods to classes
Ortes pushed a commit to Ortes/packages that referenced this pull request Jun 25, 2025
adds == and hash methods to classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: pigeon platform-android platform-ios platform-macos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants