Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't infer block return type even though there is nothing to infer #7160

Closed
panhania opened this issue Dec 8, 2018 · 0 comments · Fixed by #7161
Closed

Can't infer block return type even though there is nothing to infer #7160

panhania opened this issue Dec 8, 2018 · 0 comments · Fixed by #7161

Comments

@panhania
Copy link

panhania commented Dec 8, 2018

Consider the following snippet:

class Tree

  def initialize(@value : Int32, @children : Array(Tree))
  end

  def sum : Int32
    @value + @children.each.map(&.sum).sum
  end

end

An attempt to use Tree#sum results in the following error:

can't infer block return type, try to cast the block body with `as`. See: https://github.com/crystal-lang/crystal/wiki/Compiler-error-messages#cant-infer-block-return-type

    @value + @children.each.map(&.sum).sum
                            ^~~

We can fix it by doing what the compiler suggests, that is be replacing map(&.sum) with

map { |child| child.sum.as(Int32) }

However, this seems completely unnecessary - the type of Tree#sum is specified as Int32, so the compiler has nothing to infer and the cast should do nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants