Skip to content

Commit 2b49d71

Browse files
authored
Merge pull request scala#6720 from xuwei-k/Deferrer-value-class
Make {Stream, LazyList}.Deferrer value class
2 parents c70fbf7 + 3497725 commit 2b49d71

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/library/scala/collection/immutable/LazyList.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,15 +662,17 @@ object LazyList extends LazyListFactory[LazyList] {
662662
def unapply[A](xs: LazyList[A]): Option[(A, LazyList[A])] = #::.unapply(xs)
663663
}
664664

665-
implicit final class Deferrer[A](l: => LazyList[A]) {
665+
implicit def toDeferrer[A](l: => LazyList[A]): Deferrer[A] = new Deferrer[A](() => l)
666+
667+
final class Deferrer[A] private[LazyList] (private val l: () => LazyList[A]) extends AnyVal {
666668
/** Construct a LazyList consisting of a given first element followed by elements
667669
* from another LazyList.
668670
*/
669-
def #:: [B >: A](elem: => B): LazyList[B] = newCons(elem, l)
671+
def #:: [B >: A](elem: => B): LazyList[B] = newCons(elem, l())
670672
/** Construct a LazyList consisting of the concatenation of the given LazyList and
671673
* another LazyList.
672674
*/
673-
def #:::[B >: A](prefix: LazyList[B]): LazyList[B] = prefix lazyAppendedAll l
675+
def #:::[B >: A](prefix: LazyList[B]): LazyList[B] = prefix lazyAppendedAll l()
674676
}
675677

676678
object #:: {
@@ -831,15 +833,17 @@ object Stream extends LazyListFactory[Stream] {
831833
def unapply[A](xs: Stream[A]): Option[(A, Stream[A])] = #::.unapply(xs)
832834
}
833835

834-
implicit final class Deferrer[A](l: => Stream[A]) {
836+
implicit def toDeferrer[A](l: => Stream[A]): Deferrer[A] = new Deferrer[A](() => l)
837+
838+
final class Deferrer[A] private[Stream] (private val l: () => Stream[A]) extends AnyVal {
835839
/** Construct a Stream consisting of a given first element followed by elements
836840
* from another Stream.
837841
*/
838-
def #:: [B >: A](elem: => B): Stream[B] = newCons(elem, l)
842+
def #:: [B >: A](elem: => B): Stream[B] = newCons(elem, l())
839843
/** Construct a Stream consisting of the concatenation of the given Stream and
840844
* another Stream.
841845
*/
842-
def #:::[B >: A](prefix: Stream[B]): Stream[B] = prefix lazyAppendedAll l
846+
def #:::[B >: A](prefix: Stream[B]): Stream[B] = prefix lazyAppendedAll l()
843847
}
844848

845849
object #:: {

0 commit comments

Comments
 (0)