简体   繁体   English

在Scala中,参数类型_>:和<:是什么?

[英]What is this parameter type _>: with <: in Scala?

I have a parameterized Generic type, where the generic type is from Case classes I defined separately. 我有一个参数化的泛型类型,其中泛型类型来自我分别定义的Case类。

These are 这些是

case class TypeA(user_id: String,
                   device_id: String) extends Serializable


case class TypeB(user_id: String,
                   device_id: String,
                   org_id: String,
                   app_name: String) extends Serializable

I use a Map function to read in the datasets, based on the Generic type schema 我基于通用类型架构使用Map函数读取数据集

val res = this.readHandlersMap.map(s => s._1 match {

case "a" => {
  (s._1, s._2.read[TypeA]((Encoders.product[TypeA].schema)).asInstanceOf[Dataset[TypeA]])
}

case "b" => {
   (s._1, s._2.read[TypeB]((Encoders.product[TypeB].schema)).asInstanceOf[Dataset[TypeB]])    
}
});

After checking the output of res , I get that it is of type 检查res的输出后,我得到它的类型

Map[String, Dataset[_ >: TypeA with TypeB <: Serializable with Product]]

I don't get the meaning of this type. 我没有这种类型的意思。 What do the _>: and with s do in this type? 什么的_>:with S IN这种类型的呢? And when I try to declare res as of type Dataset[Serializable] , I get an error. 当我尝试将res声明为Dataset[Serializable]类型时,出现错误。 What will this type be if I add a TypeC ? 如果添加TypeC此类型将是什么?

These are basically upper and lower bound for the defined generic type: 这些基本上是定义的泛型类型的上限和下限:

For example: 例如:

Car <: Vehicle //it means car is subtype of vehicle 
Fruit >: Mango //fruit is super type of the mango

That means we can put an instance of car on vehicle stack. 这意味着我们可以将汽车实例放置在车辆堆栈上。 And an instance of mango on the stack of Fruit. 还有一个芒果在水果堆上的实例。

Map[String, Dataset[_ >: TypeA with TypeB <: Serializable with Product]]

This line means that Dataset is of type where. 此行表示数据集的类型为where。 _ is super type of TypeA with TypeB is the subtype of the Serializable. _是TypeA的超类型,而TypeB是Serializable的子类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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