簡體   English   中英

Scala編譯器在使用泛型的方法中說“沒有TypeTag可用於T”

[英]Scala compiler says “No TypeTag available for T” in method using generics

以下代碼未編譯:

override def read[T <: Product](collection : String): Dataset[T] = {
  val mongoDbRdd = MongoSpark.load(sparkSession.sparkContext,MongoDBConfiguration.toReadConfig(collection))
  mongoDbRdd.toDS[T]
}

這是“toDS”定義:

def toDS[T <: Product: TypeTag: NotNothing](): Dataset[T] = mongoSpark.toDS[T]()

編譯說:

Error:(11, 20) No TypeTag available for T
    mongoDbRdd.toDS[T]

Error:(11, 20) not enough arguments for method toDS: (implicit evidence$3: reflect.runtime.universe.TypeTag[T], implicit evidence$4: com.mongodb.spark.NotNothing[T])org.apache.spark.sql.Dataset[T].
Unspecified value parameters evidence$3, evidence$4.
    mongoDbRdd.toDS[T]

第11行是mongoDbRdd.toDS[T]

我真的不知道Scala Generics發生了什么,編譯器不是很具體,任何想法?

問題是與在類型約束TtoDS要求:

// The ':' constraint is a type class constraint.
def toDS[T <: Product: TypeTag: NotNothing](): Dataset[T] =
  mongoSpark.toDS[T]()

// The below is exactly the same as the above, although with user-defined
// names for the implicit parameters.
// All a type class does is append implicit parameters to your function.
def toDS[T <: Product]()(implicit typeTag: TypeTag[T], notNothing: NotNothing[T]) =
  mongoSpark.toDS[T]()

您會注意到編譯器錯誤顯示的內容 - 擴展名稱evidence$3evidence$4

如果您希望編譯方法,只需添加相同的類型類:

override def read[T <: Product: TypeTag: NotNothing](
    collection : String): Dataset[T] = { /* impl */ }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM