How to parametrize class and implement method depends on type in Scala

泄露秘密 提交于 2019-12-01 14:47:16

I have several comments:

  • check for NA outside the switch branch
  • you are missing non-NA case hence you are generating vector which is shorter than input vector (i expect you would like to generate the same length vector)

Regarding generics, you need to provide type specialization. For example, something like the following snippet:

class ReplaceNA[T](val value: T)(implicit add: TAdd[T]) extends MRTask[ReplaceNA[T]] {
  override def map(c: Chunk, nc: NewChunk): Unit = {
    for (row <- 0 until c.len()) {
      // Replace NAs by given value
      if (c.isNA(row)) {
        add.addValue(nc, value)
      } else {
        // Do something with default value
        nc.addNA()
      }
    }
  }

}

trait TAdd[T] extends Serializable {
  def addValue(nc: NewChunk, value: T)
}

object TAdd extends Serializable {
  implicit val addDouble = new TAdd[Double] { def addValue(nc: NewChunk, value: Double) = nc.addNum(value) }
  implicit val addFloat = new TAdd[Float] { def addValue(nc: NewChunk, value: Float) = nc.addNum(value) }
  implicit val addValueString = new TAdd[ValueString] { def addValue(nc: NewChunk, value: ValueString) = nc.addStr(value) }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!