内部类的spark repl“type mismatch”

时间:2016-06-27 12:58:42

标签: scala apache-spark inner-classes spark-repl

我已经在spark程序中定义了一些特性和类,它在打包到jar中时效果很好,但如果在spark repl中执行,它将会失败。

trait Builder {
  trait Layer {
    def layerSize: Int
  }

  abstract class Layer1 extends Layer

  class Layer2(val layerName: String) extends Layer {
    override def layerSize: Int = 10
    def addInput(from: Layer): String = {
      ""
    }
  }

  object Layer2  {
    def apply(name: String): Layer2 = {
      new Layer2(name)
    }
  }

  val FEATURES: Layer = new Layer1 {
    override val layerSize = 10
  }
}

class aBuilder extends Builder

然后我运行代码:

  val builder = new aBuilder
  val test = builder.Layer2("")
  test.addInput(builder.FEATURES)

错误:

<console>:32: error: type mismatch;
 found   : builder.Layer
 required: builder.Layer
                test.addInput(builder.FEATURES)

1 个答案:

答案 0 :(得分:0)

好的以下似乎适用于spark 1.6.0和官方二进制文件(scala 2.10)

scala> trait Builder {
     |   trait Layer {
     |     def layerSize: Int
     |   }
     | 
     |   abstract class Layer1 extends Layer
     | 
     |   class Layer2(val layerName: String) extends Layer {
     |     override def layerSize: Int = 10
     |     def addInput(from: Layer): String = {
     |       ""
     |     }
     |   }
     | 
     |   object Layer2  {
     |     def apply(name: String): Layer2 = {
     |       new Layer2(name)
     |     }
     |   }
     | 
     |   val FEATURES: Layer = new Layer1 {
     |     override val layerSize = 10
     |   }
     | }
// defined trait Builder

scala> class aBuilder extends Builder
// defined class aBuilder

scala> :paste
// Entering paste mode (ctrl-D to finish)
val builder = new aBuilder
val test = builder.Layer2("")
test.addInput(builder.FEATURES)
// Exiting paste mode, now interpreting.
// builder: aBuilder = $iwC$$iwC$aBuilder@8f2513c
// test: builder.Layer2 = $iwC$$iwC$Builder$Layer2@561b39f4
// res0: String = ""

尽管如此,我不确定它是否是以下问题的变体:SPARK-5149