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

Add type for Enumerator::Chain #2220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions core/enumerator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,22 @@ end
# This type of objects can be created by Enumerable#chain and Enumerator#+.
#
class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
include Enumerable[Elem]
# * `Rubocop: `self` type is not allowed in this context
# * {Enumerator::Chain#each} without block doesn't return `self`, unlike {Enumerator#each}.
#include Enumerator::_Each[Enum, self] # Workaround: def each:

# <!--
# rdoc-file=enumerator.c
# - Enumerator::Chain.new(*enums) -> enum
# -->
# Generates a new enumerator object that iterates over the elements of given
# enumerable objects in sequence.
#
# e = Enumerator::Chain.new(1..3, [4, 5])
# e.to_a #=> [1, 2, 3, 4, 5]
# e.size #=> 5
#
def initialize: (*_Each[Elem] enums) -> void

# <!--
# rdoc-file=enumerator.c
Expand All @@ -626,5 +641,10 @@ class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
#
# If no block is given, returns an enumerator.
#
def each: () { (Elem) -> void } -> void
def each: () { (Enum) -> void } -> self
| () -> Enumerator[E, self]

# wrong argument type chain (expected enumerator) (TypeError)
def with_index: (?Integer) ?{ (?) -> untyped } -> bot
def each_with_index: () ?{ (?) -> untyped } -> bot
Comment on lines +647 to +649
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby bug?

end
19 changes: 19 additions & 0 deletions test/stdlib/Enumerator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ def test_to_proc
end.next
end
end

class EnumeratorChainTest < Test::Unit::TestCase
include TestHelper

testing "::Enumerator::Chain[::Integer]"

def test_class_new
assert_send_type "(*_Each[Integer] enums) -> Enumerator::Chain[Integer]",
Enumerator::Chain, :new, 1..3, [4, 5]
end

def test_each
enum = Enumerator::Chain.new 1..3, [4, 5]
assert_send_type "() { (Integer) -> nil } -> Enumerator::Chain[Integer]",
enum, :each do end
assert_send_type "() -> Enumerator[Integer, Enumerator::Chain[Integer]]",
enum, :each
end
end
Loading