Project

General

Profile

Actions

Bug #21780

open

Change the default size of Enumerator.produce back to infinity

Bug #21780: Change the default size of Enumerator.produce back to infinity

Added by zverok (Victor Shepelev) 2 days ago. Updated about 1 hour ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:124190]

Description

In #21701 a new argument size: was introduced, and its default value is nil (unknown).

While I support the new argument, I'd argue that the default should be Float::INFINITY.

Reasoning: By design, Enumerator.produce is infinite (there is no internal condition to stop iteration), and the simplest, most straightforward usages of the method would produce definitely infinite iterators, which the user than can limit with take, or take_while or similar methods.

To produce the enumerator that will stop by itself requires explicit raising of StopIteration, which I expect to be a (slightly) advanced technique, and those who use it might be more inclined to provide additional arguments to clarify the semantics.

While Enumerator#size is hardly frequently used now (other than in #to_set, which started the discussion), it might be in the future, and I believe it is better to stick with more user-friendly defaults.

Now:

# very trivial enumerator, but if you want it to have "proper" size, you need 
# to not forget to use an elaborate argument and type additional 21 characters
Enumerator.produce(1, size: Float::INFINITY, &:succ)

# already non-trivial enumerator, which is hardly frequently used, but the 
# current defaults correspond to its semantics:
Enumerator.produce(Date.today) {
  raise StopIteration if it.tuesday? && it.day.odd?
  it + 1
}

With my proposal:

# trivial, most widespread case:
Enumerator.produce(1, &:succ).size #=> Infinity

# non-trivial case, with the enumerator designer clarifying their
# intention that "we are sure it stops somewhere":
Enumerator.produce(Date.today, size: nil) {
  raise StopIteration if it.tuesday? && it.day.odd?
  it + 1
}

Related issues 2 (0 open2 closed)

Related to Ruby - Feature #21701: Enumerator.produce accepts an optional `size` keyword argumentClosedknu (Akinori MUSHA)Actions
Related to Ruby - Bug #21654: Set#new calls extra methods compared to previous versionsClosedActions
Actions

Also available in: PDF Atom