为什么 Scala 枚举在 Apache Zeppelin 中不起作用,但在 maven 中起作用 [英] Why Scala Enumeration does not work in Apache Zeppelin but it works in maven
问题描述
当我在 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)中.为了使用 Enumeration 或 seal&object,它应该被包裹在 object 中而不是直接定义在根作用域上.
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 中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!