match may not be exhaustive. It would fail on the following input: None
原因
模式匹配错误导致的scale的语法错误,
跟eclipse无关
解决方法
以下面代码为例:
Option(Session.get().getAttribute("player")) match {
case None => {
val player = new Player(user.getEmail, user.getNickname).createOrGet
Session.get().setAttribute("player", player)
}
}
报错同样为:
Warning:(35, 11) match may not be exhaustive.
It would fail on the following input:
这段代码错误的原因在于:
模式匹配时,您应该考虑所有可能的情况或提供“fallback”(case _ => ...)。Option可以是Someor None,但您只匹配None大小写。
如果Session.get().getAttribute("player")返回Some(player),您将得到一个MatchError(异常)。
由于您的代码似乎没有返回任何内容,因此我将在没有的情况下重新编写它match,只需检查isEmpty.
if(Option(Session.get().getAttribute("player")).isEmpty) {
val player = new Player(user.getEmail, user.getNickname).createOrGet
Session.get().setAttribute("player", player)
}
尽管这与检查并没有太大区别Session.get().getAttribute("player") == null。
匹配可能不是详尽无遗的。如果输入以下信息,它就会失败: none
case None=> "Don't know." //如果可选类型为None 返回don't know