我剛剛開始使用Scala,所以請原諒,如果事實證明這很愚蠢。在Scala程序中找不到值
我想在Scala中實現一個鏈表。所以,我創建了一個包含MYLIST類和其他文件中MyList.scala
:
abstract class MyList[+A] {
def head : A
def tail : MyList[A]
def isEmpty : Boolean
def add[B>:A](element: B) : MyList[B] = new MyNonEmptyList[B](element,this)
}
object MyEmptyList extends MyList[Nothing]{
override def head: Nothing = throw new NoSuchElementException("Head of an Empty list : ")
override def tail: MyList[Nothing] = throw new NoSuchElementException("Tail of an Empty list!")
override def isEmpty: Boolean = true
override def toString : String = ""
}
case class MyNonEmptyList[A] (head: A, tail:MyList[A]) extends MyList[A]{
override def isEmpty: Boolean = false
override def toString : String = head + ", " + tail.toString
}
雕刻出的基本功能後,是時候對其進行測試。所以,我創建了一個階工作test.sc
:
object test{
val list = MyNonEmptyList(1,MyEmptyList)
val b = list.head
}
現在,當我嘗試運行的代碼(上的IntelliJ),我收到以下錯誤:
Error:(1, 18) not found: value MyNonEmptyList
lazy val list = MyNonEmptyList(1,MyEmptyList)
^
我不明白背後的原因這個錯誤。當我按Ctrl +單擊時,我被重定向到MyList.scala文件中的源代碼。
可以分享.scala文件的位置和test.sc –