Scala.NotImplementedError:缺少实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18901999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:42:18  来源:igfitidea点击:

Scala.NotImplementedError: an implementation is missing?

scala

提问by Michael

Here is my code:

这是我的代码:

package example

object Lists {

  def max(xs: List[Int]): Int = {
    if(xs.isEmpty){
        throw new java.util.NoSuchElementException()
    }
    else {
        max(xs.tail)
    }
  }
}

When i run it in sbt console:

当我在 sbt 控制台中运行它时:

scala> import example.Lists._
scala> max(List(1,3,2))

I have the following error:

我有以下错误:

Scala.NotImplementedError: an implementation is missing

How can I fix that?

我该如何解决?

Thanks.

谢谢。

回答by Shen Qi

open example.Lists, you will see below lines:

打开 example.Lists,您将看到以下几行:

def sum(xs: List[Int]): Int = ???
def max(xs: List[Int]): Int = ???

use 0instead of ???.

使用0代替???.

回答by rajnish

Also, putting the correct recursive implementation for the max that should work

此外,为应该工作的最大值放置正确的递归实现

  def max(xs: List[Int]): Int = {
    if(xs.isEmpty){
      throw new java.util.NoSuchElementException()
    }
    val tailMax =  if (xs.tail.isEmpty)  xs.head else max(xs.tail)
    if (xs.head >= tailMax){
      xs.head
    }
    else  tailMax;
  }

回答by Jan Clemens Stoffregen

I had the same problem because I did not quit the Scala console (in IntelliJ) from sbt with the command:

我遇到了同样的问题,因为我没有使用以下命令从 sbt 退出 Scala 控制台(在 IntelliJ 中):

scala> :q

and then restarted the the console from sbt, so that everything could be compiled again.

然后从 sbt 重新启动控制台,以便可以再次编译所有内容。

> console

It`s not necessary to restart sbt, though.

不过,没有必要重新启动 sbt。

回答by u6624954

I have suffered the same problem. But I have fixed it.

我遇到了同样的问题。但我已经修好了。

The solution is that you should restart your "sbt console", and import the module again, it works fine.

解决方案是您应该重新启动“sbt 控制台”,然后再次导入模块,它工作正常。