I have the following for learning.
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: _*))
}
When I load the REPL via sbt console
and enter for example
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, ?)
It uses Stream from scala.collection.immutable in the two first cases instead of mine. How can I make sbt only use mine?
In the SBT console, import your class before attempting to use it:
scala> import learning.laziness.Stream
scala> Stream(1, 2)
scala> //etc.
Your Stream
class's code must be under an SBT source folder (by default, this would be src/main/scala
, relative to the project root directory, or in a custom source directory specified by an SBT scalaSource in Compile
directive - it will not be found and compiled by SBT otherwise). As the code you provide is in the package learning.laziness
, then the default location for your Stream.scala
source file would need to be src/main/scala/learning/laziness
. Once SBT knows where to find your file, you should then be fine.