Why Scala Enumeration does not work in Apache Zeppelin but it works in maven

流过昼夜 提交于 2019-12-12 04:39:13

问题


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)
}

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?


回答1:


I figured out the trick to solve this issue.

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.

The reason why it works in maven is that I already put it into an object.

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)
}

Then use it in a Zeppelin paragraph

import t._
f(DashStyle.Solid)


来源:https://stackoverflow.com/questions/38484154/why-scala-enumeration-does-not-work-in-apache-zeppelin-but-it-works-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!