Skip to content

Incorrect understanding of max_by return type #777

Description

@amake

The following code:

# @return [Integer]
def foo
  [1, 2, 3].max_by(&:abs)
end

Fails to type check with the following error:

% solargraph typecheck --level typed tmp.rb
tmp.rb:2 - Declared return type Integer does not match inferred type Enumerator<Integer, Integer> for #foo
1 problem found.

Looking at the RBS it seems like it does indicate that a single object can be returned:

https://github.com/ruby/rbs/blob/97ed5d9492b9d2050b87dd4b247be4097e970b9d/core/enumerable.rbs#L973-L976

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions