Enumerable#include? should receive any object#2849
Enumerable#include? should receive any object#2849HoneyryderChuck wants to merge 1 commit intoruby:masterfrom
Conversation
this checks only if an object exists in the collection, regardless of type. ruby does not raise an exception if the types don't match.
|
This behavior is intentional. The goal is to detect more type errors arising from a commonly seen pattern like this: hash = { foo: :bar } #: Hash[Symbol, Symbol]
hash["foo"] # This is usually a mistake.I'm open to discuss this issue here to have some conclusion. Do you have any practical examples or real-world scenarios where the proposed one worked better? (From a type-theoretic perspective, the current implementation (using |
|
It'd be nice if we could have a signature like: And then that way whenever you use |
|
Oh, we do. I opened #1875 a year and a half ago, and that was suggested even before that. |
|
@soutaro any place where you don't control where the argument comes from. I caught this while checking one of the many steep errors i still get on the httpx codename. Fwiw there are other methods where I'd apply the same reasoning, like Hash#key?, and while I'd expect enforcing typing while make some code a bit more convoluted, I don't see the upside in this case. |
def []: (K) ->V?
| (untyped) -> nilThis doesn't work well in static typing. Having hash = { foo: 123 }
key = :foo #: top
hash[key] # => nil |
|
@sampersand meant this, @soutaro. def []: ((K) -> V) & ((untyped) -> nil) |
Like receiving network request or loading from files? I was assuming those values have |
|
@soutaro I'm having a hard time finding the example that motivated me to open this PR. Most of the examples I find in the codebase are of values which are extracted from arrays/strings/via-regexps which may be smth or nil, and due to the "or nil", don't pass the check. One could argue that I could "skip if nil" before doing the include? call, but that results in rather tedious checks, specially if the include? calls are in the middle of if expressions. Neverthesless, I'll leave you with this use case, that you may consider more "real world": # @rbs
SEMAPHORE_LIGHTS: Array[:red | :yellow | :green]
SEMAPHORE_LIGHTS = %i[red yellow green]
SEMAPHORE_LIGHTS.include?(:blue)Output I can't find of a sane way of skipping that. |
this checks only if an object exists in the collection, regardless of type. ruby does not raise an exception if the types don't match.