【发布时间】:2017-12-18 22:11:01
【问题描述】:
我有以下学习。
package learning.laziness
sealed trait Stream[+A] {
def headOption: Option[A] = this match {
case Empty => None
case Cons(h, _) => Some(h())
}
def toList: List[A] = this match {
case Empty => List.empty
case Cons(h,t) => h()::t().toList
}
}
case object Empty extends Stream[Nothing]
case class Cons[A](head: () => A, tail: () => Stream[A]) extends Stream[A]
object Stream {
def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
lazy val head = hd
lazy val tail = tl
Cons(() => head, () => tail)
}
def empty[A]: Stream[A] = Empty
def apply[A](as: A*): Stream[A] =
if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*))
}
当我通过sbt console 加载 REPL 并输入示例时
-
Stream(1,2)res0: scala.collection.immutable.Stream[Int] = Stream(1, ?)
-
Stream.apply(1,2)res1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
-
Stream.cons(1, Stream.cons(2, Stream.empty))res2: Stream.Cons[Int] = Stream(1, ?)
它在前两种情况下使用来自 scala.collection.immutable 的 Stream 而不是我的。我怎样才能让 sbt 只使用我的?
【问题讨论】:
标签: scala sbt read-eval-print-loop