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

Enumerator#initialize support nil, #call and #to_int #1904

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
6 changes: 5 additions & 1 deletion core/enumerator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ class Enumerator[unchecked out Elem, out Return] < Object
#
def feed: (Elem arg0) -> NilClass

interface _Call
def call: () -> untyped
end
Comment on lines +272 to +274
Copy link
Contributor

Choose a reason for hiding this comment

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

Enumerator.new(->{'hi'}) {}.size #=> "hi"
Suggested change
interface _Call
def call: () -> untyped
end
interface _Size
def call: () -> (Integer | Float)?
end

Relevant: #420 (comment)


# <!--
# rdoc-file=enumerator.c
# - Enumerator.new(size = nil) { |yielder| ... }
Expand All @@ -293,7 +297,7 @@ class Enumerator[unchecked out Elem, out Return] < Object
# lazy fashion (see Enumerator#size). It can either be a value or a callable
# object.
#
def initialize: (?Integer arg0) { (Enumerator::Yielder arg0) -> Return } -> void
def initialize: (?(nil | _Call | ::_ToInt) arg0) { (Enumerator::Yielder arg0) -> Return } -> void
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def initialize: (?(nil | _Call | ::_ToInt) arg0) { (Enumerator::Yielder arg0) -> Return } -> void
def initialize: (?(int? | _Call) size) { (Yielder yielder) -> Return } -> void


# <!--
# rdoc-file=enumerator.c
Expand Down
15 changes: 15 additions & 0 deletions test/stdlib/Enumerator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ class EnumeratorSingletonTest < Test::Unit::TestCase
def test_new
assert_send_type "() { (Enumerator::Yielder) -> 12345 } -> Enumerator[untyped, 12345]",
Enumerator, :new do 12345 end

assert_send_type "(123) { (Enumerator::Yielder) -> nil } -> Enumerator[untyped, nil]",
Enumerator, :new, 123 do nil end

o = Object.new
def o.call = 'hi'
assert_send_type "(Enumerator::_Call) { (Enumerator::Yielder) -> nil } -> Enumerator[untyped, nil]",
Enumerator, :new, o do nil end
assert_send_type "(Enumerator::_Call) { (Enumerator::Yielder) -> nil } -> Enumerator[untyped, nil]",
Enumerator, :new, ->{'hi'} do nil end

with_int(123) do |int|
assert_send_type "(_ToInt) { (Enumerator::Yielder) -> nil } -> Enumerator[untyped, nil]",
Enumerator, :new, int do nil end
end
end

def test_produce
Expand Down