Skip to content

Changed signature of Matcher.matches(Object) to matches(T) #200

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

Closed
wants to merge 1 commit into from
Closed

Changed signature of Matcher.matches(Object) to matches(T) #200

wants to merge 1 commit into from

Conversation

krichter722
Copy link
Contributor

@krichter722 krichter722 commented Apr 1, 2018

…parameter of the type.

The method comment indicated that this design decision was the result of a misunderstanding of how Java Generics work. The change increases type-safety while users preferring a type-unsafe approach can still omit the generic from the static type of their declarations.

Unused tests which no longer make sense have been removed.

WIP: bean-related tests fail and it's very hard to figure out how to pass matchers down the bean graph which formerly had the wrong type (probably returned because they where TypeSafeMatchers)

Closes #199

Unverified

This user has not yet uploaded their public signing key.
…parameter of the type.

The method comment indicated that this design decision was the result of a misunderstanding of how Java Generics work. The change increases type-safety while users preferring a type-unsafe approach can still omit the generic from the static type of their declarations.

Unused tests which no longer make sense have been removed.

WIP: one test fails and it's very hard to figure out how to pass matchers down the bean graph which formerly had the wrong type (probably returned because they where TypeSafeMatchers)

Closes #199
@sf105
Copy link
Member

sf105 commented Apr 1, 2018

The interface uses matches(Object) because we don't want to over-restrict how a Matcher is used. We don't necessarily want to bind together where the matcher and matchee are declared. This is equivalent to the equals(Object) method on Object, which obviously could be parameterised but isn't. When experimenting with alternatives, we found that there's a usability problem with generics in practice which can lead to lots of tedious type declarations that add little to the effectiveness of a match.

I appreciate your contribution, but I suggest not making assumptions about the authors' understanding.

Thanks

@sf105 sf105 closed this Apr 1, 2018
@krichter722
Copy link
Contributor Author

krichter722 commented Apr 1, 2018

Thanks for your quick feedback.

I appreciate your contribution, but I suggest not making assumptions about the authors' understanding.

Then you should write that explanation in the Javadoc comment instead of one that leaves the reader with that impression about the understanding of the author. Especially the finding on use cases where this design has advantages should definitely be listed there because it's very helpful at this location (in case you have it somewhere else, please add a link instead).

The interface uses matches(Object) because we don't want to over-restrict how a Matcher is used. We don't necessarily want to bind together where the matcher and matchee are declared. This is equivalent to the equals(Object) method on Object, which obviously could be parameterised but isn't. When experimenting with alternatives, we found that there's a usability problem with generics in practice which can lead to lots of tedious type declarations that add little to the effectiveness of a match.

The idea of generics is to avoid casting and eventual exceptions - not so much for the sake of effectiveness, but type-safety and static analysis - the code becomes more effective because it's a lot easier to maintain, not in an algorithmic runtime-oriented sense. Like I explained, the changes have no downside and don't add any overhead for users which want to use Matcher instead of Matcher<T>, but allow user who wish to write type-safe code - which implies the necessary overhead; e.g. the missing feature to call static methods with type parameter is a real bummer in Java since having to write Matcher.<String>any() instead of any() can be tedious indeed, however it's not necessary.

In order to be able to apply the same argument for Matcher that is used for Object.equals you would have to remove the type parameter because it's unused, I think, but it's a good point - however, Object.equals is special for many reasons that don't apply to Matcher.matches.

@krichter722
Copy link
Contributor Author

For what it's worth, I'm using hamkrest for projects which I migrated to Kotlin already which solves the problem I was trying to solve here.

Sorry, something went wrong.

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.

Matcher.matches needs to have parameterized argument
2 participants