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

Remove Object#=~ and Object#!~ #5910

Conversation

straight-shoota
Copy link
Member

Fixes #5829

Overloads for Regex#=~ are restricted to receive String | Nil and for String#=~ to receive Regex | Nil. #!~ is re-added as instance method in both classes accepting the same overload.

It could be up for debate to leave Object#!~ to avoid having individual definitions in classes implementing =~. But this would end up in a similar issues as #3450

I'm also not entirely sure about the type safety of Regex#=~ and String#=~. Should their argument really be nilable, or allow only String, Regex type, respectively? On the other hand, they could even receive any type and just return nil like it is the case right now.

@RX14
Copy link
Contributor

RX14 commented Apr 3, 2018

I'm sure you'll realise why this was the way it was as you try to make CI pass.

@@ -170,7 +170,7 @@ module Spec
end

def match(actual_value)
actual_value =~ @expected_value
actual_value.responds_to?(:=~) && actual_value =~ @expected_value
Copy link
Contributor

Choose a reason for hiding this comment

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

Not really a fix: you moved the "returns nil" logic from global operators to a specific use case.

The fact that the match expectation is called with non String nor RegExp objects may be proof that the global behavior is actually good?

@straight-shoota
Copy link
Member Author

@ysbaddaden True, I was just checking f there would be any other failures.

It seems it is just missing Nil#=~(String | Regex). It would probably be fine to have this method for commutativity with (String | Regex)#=~(Nil). I don't think it is necessary to have a default implementation for any type.

Although the issue is that Nil#=~(other) can't be restricted to String | Regex because other types might implement the matcher as well. If there is no type restriction at all, it defeats the purpose of stronger type safety. Possible solution would be a module Matchable (ugly) or a macro to check if other implements #=~.

Both are not optimal. I don't know if there is a better way. I still would like to avoid having #=~ on every object.

@RX14
Copy link
Contributor

RX14 commented Apr 5, 2018

sure nil is pretty common but it's not really specil compared to any other class. We should keep this as-is.

I vote close.

@asterite
Copy link
Member

I also vote to close it. I'll explain (a bit more) why in #5829

@asterite asterite closed this Apr 28, 2018
@straight-shoota straight-shoota deleted the jm/feature/remove-Object-pattern-matcher branch April 28, 2018 14:27
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.

None yet

4 participants