【发布时间】:2017-08-15 14:18:31
【问题描述】:
在 IntelliJ 上运行我的 Scala 代码时出现错误(使用 Scala 2.11.8):
package week4
/**
* Created by BigB on 15/08/17.
*/
trait List[T] {
def isEmpty: Boolean
def head: T
def tail: List[T]
}
class Cons[T](val head: T, val tail: List[T]) extends List[T] {
override def isEmpty: Boolean = false
}
class Nil[T] extends List[T] {
override def isEmpty: Boolean = true
override def head: T = throw new NoSuchElementException("Nil.head")
override def tail: List[T] =throw new NoSuchElementException("Nil.tail")
}
我的 Scala 工作表有:
import week4._
object nth {
def nth[T](n: T, l: List[T]): T = {
if (l.isEmpty) throw new IndexOutOfBoundsException
else if (n==0) l.head
else nth(n-1, l.tail)
}
val l1 = new Cons(1, new Cons(2, new Cons(2, new Nil)))
nth(2, l1)
}
错误:
错误:(9, 20) not found: type Cons 惰性 val l1 = new Cons(1, new Cons(2, new Cons(2, new Nil))) ^
错误:(6, 16) 值 - 不是类型参数 T 的成员 否则 nth(n-1, l.tail) ^
【问题讨论】:
-
你必须先编译 week4 包,然后尝试运行工作表。至少它对我有用,无需任何代码更改
标签: scala intellij-idea