简体   繁体   中英

Why can't traits have parameters with Context Bounds

Here :ClassTag : TypeTag base class definitions are not allowed on traits as they are considered as Context Bounds

I can make it Abstract Class but I loose the benefit of multi-inheritance

import scala.reflect.ClassTag
import reflect.runtime.universe.TypeTag
import org.apache.spark.sql.Dataset

trait DataProcessor[T <: Product : ClassTag : TypeTag, U <: Product : ClassTag : TypeTag] {
  def performAnalysis(inputDs: Dataset[T]): Dataset[U]
}

This is due to the fact that Scala doesn't allow a trait to receive arguments as they don't have a constructor (this might change in the upcoming future ). An expansion of context bounds is to add implicit parameters to the definition. Thus, you're actually trying to write:

trait DataProcessor[T <: Product, U <: Product](implicit ev: ClassTag[T], ev1: TypeTag[U], ...)

Instead, you can require them as abstract type members on the trait:

trait DataProcessor[T <: Product, U <: Product] {
  def typeTagU: TypeTag[U]
  def clsTagU: ClassTag[U]
  def typeTagT: TypeTag[T]
  def clsTagT: ClassTag[T]

  def performAnalysis(inputDs: Dataset[T]): Dataset[U]
}

Or, as Luis mentioned, move the implicits to where you actually require them, at the method level:

def performAnalysis(inputDs: Dataset[T])(implicit ev: ClassTag[T], ev1: TypeTag[T]): Dataset[U]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM