Could you help me understand the following behaviour ?
# with 0.33.0
#=> (Float64 | Int32)
How can I get Int32 ? It looks like it entered the rescue clause.
If I add an else clause, it seems to go through both clauses :
#=> (Int32 | String)
The reasoning the first example is a union of
Float64 | Int32 is because those are the types that could possibly be returned. I.e. the compiler doesn’t actually know if there will be an exception or not since that happens at runtime. If there is no exception, then the value in the
begin portion of it is returned, which is a
Float64. If there is an exception, then the value in the
rescue part of it is returned, which is an
Int32. Thus, the compiler is telling you the resulting value of this statement can be
The second example is similar. However, in this case
Float64 is removed from the union since the statement will never return the value within the
begin portion. The reason for this is because of what the
else clause is executed only if no exceptions were rescued:
If there is no exception the value in the
else portion would be used, which would be a
String, otherwise the value in the
rescue portion would be used, which is an
Int32. Thus resulting in your union of
String | Int32.
Thank you for your answer :)