Skip to content
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

Implement operator== and hashCode for Rect. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Implement operator== and hashCode for Rect. #1

wants to merge 1 commit into from

Conversation

thomcc
Copy link

@thomcc thomcc commented Sep 21, 2014

I've implemented equality and hashCode operations for Rect. I wasn't sure if it was better to compare based on top/left/bottom/right, or pos/size, but went with pos/size because those are externally visible (It would be pretty crappy for r1 == r2 to be true but r1.x == r2.x not to be).

The downside of this is that now two Rects might contain an identical set of points (err, Vecs), but not compare as equal.

I have tests for the operator== but not for the hashCode because I'm not sure it would make sense to test hashCode.

@munificent
Copy link
Owner

I wasn't sure if it was better to compare based on top/left/bottom/right, or pos/size, but went with pos/size because those are externally visible

Normally, I think that would be the right call since those are what Rect actually stores.

The downside of this is that now two Rects might contain an identical set of points (err, Vecs), but not compare as equal.

It will do the right thing if you construct a Rect using Vec, but not if you use Direction (which implements Vec). This is because of an annoying limitation in Dart: you can't switch on a type that implements ==.

Being able to use Directions in a switch is obviously really useful. But it's also quite useful to be able to treat a Direction like unit-distance Vec. So I do this weird workaround where there's a VecBase that has most of the functionality but no == and Vec and Direction extend that. Vec does add == on top of that, but Direction doesn't.

So, now that I think about it, testing the actual corners is probably best in Rect since that handles Direction too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants