Compiler error messages - nob-suz/crystal GitHub Wiki
can't infer block return type
For example:
class Foo
def initialize(@name)
end
def name
@name
end
end
a = [] of Foo
x = a.map { |f| f.name } #=> error: can't infer block type, try to cast the block body with `as`
Here Foo
was never instantiated so the compiler has no way of knowing what the type of @name
is.
To solve this, cast the block body with as
:
x = a.map { |f| f.name as String } # works
In the future we want to get rid of this error messages and make the compiler smarter. In the above case @name
could be deduced to be Void
or NoReturn
, but for now you have to use this workaround.