Amita:
I have just started with Scala so please forgive if this turns out to be silly.
I am trying to implement a linked list in Scala. So, I created a file that contains MyList class and others in 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
}
After carving out the basic functionalities, it was time to test it. So, I created a scala worksheet test.sc
:
object test{
val list = MyNonEmptyList(1,MyEmptyList)
val b = list.head
}
Now, when I try to run the code (on IntelliJ), I get the following error:
Error:(1, 18) not found: value MyNonEmptyList
lazy val list = MyNonEmptyList(1,MyEmptyList)
^
I do not get the reason behind this error. When I do ctrl+click, I am redirected to the source in MyList.scala file.
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE