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
Type-matching with case
doesn't downcast with >= 3 types
#8864
Comments
Also happens when the condition is a union type: value = 1.as(String | Int32 | Int64 | Nil)
case value
when Int32 | String
typeof(value) # => (Int32 | Int64 | String | Nil)
end I suppose conditions consisting of one or two types are transformed into |
It's confusing so maybe we should make that work. But commas is the way to use it. |
Also, for the original snippet, that case is rewritten to: value = 1.as(String | Int32 | Int64 | Nil)
if value.is_a?(Int32) || value.is_a?(Int64) || value.is_a?(String)
# ...
end
I think we should remove that because in the general case there's no way to make it work. So you can't do |
What about |
I don't know. |
@asterite Would you care to explain why not? |
That is not equivalent to class Foo; end
class BarA < Foo; end
class BarB < Foo; end
class BarC < Foo; end
x = BarA.new || BarB.new || BarC.new
x.is_a?(BarB) || x.is_a?(BarC) # false
x.is_a?(BarB | BarC) # true
x.is_a?(Union(BarB, BarC)) # true
x.is_a?(Foo) # true On the other hand: x = BarA.new
x.is_a?(BarB) || x.is_a?(BarC) # false
x.is_a?(BarB | BarC) # false
x.is_a?(Union(BarB, BarC)) # true
x.is_a?(Foo) # true |
I think some results are unexpected and I consider them bugs. |
Given the following code:
In the first branch of the top-level
case
statement (checking the return value offoo
), the compile-time type of the return value is all 4 return types despite being cast down to 3 types. Theelse
clause also seems to be wrong. In that branch, the type can only beNil
, but the compiler thinks it can also beInt32
orString
.It works as expected if, instead of 3 or more types in the
when
clause, I use 1 or 2, and whetherNil
is one of those types doesn't seem to have an effect. For example:The text was updated successfully, but these errors were encountered: