Skip to content

Compiler error messages

Ary Borenszweig edited this page Jan 27, 2015 · 1 revision

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.

Clone this wiki locally