Skip to content

Wide NumericRange.iterator could avoid IllegalArgumentException: More than Int.MaxValue elements. #13100

@retronym

Description

@retronym

Reproduction steps

Scala version: 2.13.16

scala> try { ((0L).until(Int.MaxValue.toLong + 1)).iterator } catch { case ex: IllegalArgumentException => ex.printStackTrace }
java.lang.IllegalArgumentException: More than Int.MaxValue elements.
	at scala.collection.immutable.NumericRange$.check$1(NumericRange.scala:433)
	at scala.collection.immutable.NumericRange$.count(NumericRange.scala:443)
	at scala.collection.immutable.NumericRange.length$lzycompute(NumericRange.scala:76)
	at scala.collection.immutable.NumericRange.length(NumericRange.scala:76)
	at scala.collection.immutable.NumericRange.last(NumericRange.scala:84)
	at scala.collection.immutable.NumericRange$NumericRangeIterator.<init>(NumericRange.scala:530)
	at scala.collection.immutable.NumericRange.iterator(NumericRange.scala:56)

Problem

In the spirit of the fix in scala/scala#10259, NumericRangeIterator could possible be reworked in terns of crossesTheEndAfterN to avoid calling length and tripping the exception

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions