You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
require"benchmark"deffoo(ary : Array(Float64))
return ary
end
ary = [1, 2, 3]
ary = ary.map { |n| n.to_f }
Benchmark.ips do |x|
x.report("foo") { foo(ary) }
end
fails to compile with an error:
no overload matches 'foo' with types (Array(Int32) | Array(Float64))
I thought it may be related to invoking the function inside a block, but no, I could only produce this error while using Benchmark.ips, e.g. the following code compiles as expected:
(0..10).each do |i|
10.times do
foo(ary)
endend
The text was updated successfully, but these errors were encountered:
Benchmark.ips captures the block, so it gets turned into a Proc. So now we have a Proc referencing var. We (the compiler) don't know when it will run, so we have to consider all types var might be as valid. In your second example the second assignment overrides the type of the first assignment since there it's guaranteed that foo is called after it happened.
Since it's quite impossible to do such analysis to the level where the compiler understands that some asynchronous execution may only happen after a certain event happened, this is probably a won't fix. Easy workaround: use another variable name so you never get a union: http://carc.in/#/r/flt
I'll leave this open, it's not a bug (it works because the variable is captured) but we can definitely try to improve this.
jhass
changed the title
Type inference issue (with Benchmark.ips)
Trace Proc execution to avoid unions of captured variables (Was: Type inference issue (with Benchmark.ips))
Sep 18, 2015
The following code:
fails to compile with an error:
I thought it may be related to invoking the function inside a block, but no, I could only produce this error while using
Benchmark.ips
, e.g. the following code compiles as expected:The text was updated successfully, but these errors were encountered: