This code fails to compile because the type of t
is just Nil
and it does not have #[]
at the first
type reduction, I think.
def foo(&)
t = nil
(1..10).each do |i|
t = yield(t, i)
end
t
end
ret = foo do |t, i|
a = -> do
{ a: i, b: (t ? t[:b] : nil) }
end
b = -> do
{ a: (t ? t[:a] : nil), b: i }
end
if rand > 0.2
a.call
elsif rand > 0.2
b.call
else
t
end
end
pp! ret
Showing last frame. Use --error-trace for full trace.
error in line 11
Error: undefined method '[]' for Nil
https://play.crystal-lang.org/#/r/9hd0
But, the following code which same except without proc compiles fine.
def foo(&)
t = nil
(1..10).each do |i|
t = yield(t, i)
end
t
end
ret = foo do |t, i|
if rand > 0.2
{ a: i, b: (t ? t[:b] : nil) }
elsif rand > 0.2
{ a: (t ? t[:a] : nil), b: i }
else
t
end
end
pp! ret
ret # => {a: 10, b: 1}
https://play.crystal-lang.org/#/r/9hd2
What is different here?