Skip to content

Conversation

@NthPortal
Copy link
Contributor

@NthPortal NthPortal commented May 14, 2020

Change #:: for LazyList so that the tail computation
is wrapped as a state computation for a new LazyList,
thus deferring its execution as long as possible.

Fixes scala/bug#11984


Before (2.13.2-):

scala 2.13.2> 1 #:: { println("hey"); LazyList() }
val res4: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.2> res1.head
hey
val res5: Int = 1

After (2.13.3+):

scala 2.13.3> 1 #:: { println("hey"); LazyList() }
val res4: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.3> res1.head
val res5: Int = 1

@NthPortal NthPortal added library:collections PRs involving changes to the standard collection library bug fix labels May 14, 2020
@scala-jenkins scala-jenkins added this to the 2.13.4 milestone May 14, 2020
@NthPortal NthPortal requested review from SethTisue and sjrd May 14, 2020 05:35
@NthPortal NthPortal modified the milestones: 2.13.4, 2.13.3 May 14, 2020
@NthPortal
Copy link
Contributor Author

Interestingly, #::: is already sufficiently lazy and doesn't need to be changed. I found it quite difficult to reason about this, however, and was only fully convinced of it after writing a test to prove it.

Change `#::` for `LazyList` so that the tail computation
is wrapped as a state computation for a new `LazyList`,
thus deferring its execution as long as possible.
@NthPortal NthPortal force-pushed the topic/ll-cons-laziness/PR branch from 12e3577 to 905eabc Compare May 14, 2020 06:51
@SethTisue SethTisue added the release-notes worth highlighting in next release notes label May 14, 2020
@NthPortal NthPortal requested a review from julienrf May 16, 2020 21:50
@SethTisue SethTisue merged commit a8a7261 into scala:2.13.x May 20, 2020
@NthPortal NthPortal deleted the topic/ll-cons-laziness/PR branch May 20, 2020 05:00
@SethTisue
Copy link
Member

@NthPortal should LazyList.cons have changed as well? see discussion at https://www.reddit.com/r/scala/comments/hgv8k3/lazylist_or_lazylistcons/

scala 2.13.3> 1 #:: { println("hey"); LazyList() }
val res4: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.3> res1.head
val res5: Int = 1

scala 2.13.3> LazyList.cons(1, { println("hey"); LazyList() })
val res6: scala.collection.immutable.LazyList[Int] = LazyList(<not computed>)

scala 2.13.3> res6.head
hey
val res7: Int = 1

@NthPortal
Copy link
Contributor Author

yeah, probably :(

@NthPortal
Copy link
Contributor Author

see #9095

ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Jul 5, 2020
ijuma added a commit to ijuma/scala-library-compat that referenced this pull request Aug 24, 2020
hamzaremmal pushed a commit to hamzaremmal/scala3 that referenced this pull request May 2, 2025
…ness/PR

Increase laziness of #:: for LazyList
hamzaremmal pushed a commit to scala/scala3 that referenced this pull request May 7, 2025
…ness/PR

Increase laziness of #:: for LazyList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

library:collections PRs involving changes to the standard collection library release-notes worth highlighting in next release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LazyList.isEmpty forces tail

5 participants