Skip to content

History

New page
Showing with 0 additions and 28 deletions.
  1. +0 −28 Compiler-error-messages.md
28 changes: 0 additions & 28 deletions Compiler-error-messages.md
Original file line number Diff line number Diff line change
@@ -1,28 +0,0 @@
# can't infer block return type

For example:

```crystal
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`:

```crystal
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.