问题描述
在Maven项目(具有相同的Scala版本)中使用枚举时,枚举可以正常工作.
Enumeration works as expected when I use it in a maven project(with the same Scala version).
object t {
object DashStyle extends Enumeration {
val Solid,ShortDash = Value
}
def f(style: DashStyle.Value) = println(style)
def main(args: Array[String]) = f(DashStyle.Solid)
}
但是当它在Apache Zeppelin(Zeppelin 0.6,Spark 1.6,Scala 2.10,Java 1.8)中运行时
But when it runs in Apache Zeppelin(Zeppelin 0.6, Spark 1.6, Scala 2.10, Java 1.8)
object DashStyle extends Enumeration {
val Solid,ShortDash = Value
}
def f(style: DashStyle.Value) = println(style)
f(DashStyle.Solid)
即使报告说找到的类型和所需的类型完全相同,它也会报告以下错误
It reports the following error even it says found and required type is exactly the same
<console>:130: error: type mismatch;
found : DashStyle.Value
required: DashStyle.Value
f(DashStyle.Solid)
为什么以及如何使用它?
Why and how should I use it?
推荐答案
我想出了解决此问题的技巧.
I figured out the trick to solve this issue.
在Apache Zeppelin(或Scala REPL)中.为了使用枚举或密封对象,应将其包装在对象中,但不能直接在根范围上定义.
In Apache Zeppelin (or Scala REPL). In order to use Enumeration or sealed&object, it should be wrapped in object but not directly define on the root scope.
它在maven中起作用的原因是我已经将其放入对象中.
The reason why it works in maven is that I already put it into an object.
在Zeppelin段落中定义对象的枚举
Define enumeration in an object in a Zeppelin paragraph
object t {
object DashStyle extends Enumeration {
val Solid,ShortDash = Value
}
def f(style: DashStyle.Value) = println(style)
}
然后在Zeppelin段落中使用它
Then use it in a Zeppelin paragraph
import t._
f(DashStyle.Solid)
这篇关于为什么Scala枚举在Apache Zeppelin中不起作用,但在Maven中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!