Scala Type Parameters 2
类型关系
Scala 支持在泛型类上使用型变注释,用来表示复杂类型、组合类型的子类型关系间的相关性
协变
+T,变化方向相同,通常用在生产假设
A extends T, 对于Clazz[+T],则Clazz[A]也可看做Clazz[T]// 官网示例
abstract class Animal {
def name: String
}
case class Cat(name: String) extends Animal
case class Dog(name: String) extends Animal
由于 Scala 标准库中不可变
List的定义为List[+A],因此List[Cat]是List[Animal]的子类型,List[Dog]也是List[Animal]的子类型,所以可直接将他们当作List[Animal]使用。// 官网示例
object CovarianceTest extends App {
def printAnimalNames(animals: List[Animal]): Unit = {
animals.foreach { animal =>
println(animal.name)
}
} val cats: List[Cat] = List(Cat("Whiskers"), Cat("Tom"))
val dogs: List[Dog] = List(Dog("Fido"), Dog("Rex")) printAnimalNames(cats)
// Whiskers
// Tom printAnimalNames(dogs)
// Fido
// Rex
}
逆变
-T,变化方向相反,通常用在消费假设
A extends T, 对于Clazz[-T],则Clazz[T]也可看做Clazz[A]// 官网示例
abstract class Printer[-A] {
def print(value: A): Unit
} class AnimalPrinter extends Printer[Animal] {
def print(animal: Animal): Unit =
println("The animal's name is: " + animal.name)
} class CatPrinter extends Printer[Cat] {
def print(cat: Cat): Unit =
println("The cat's name is: " + cat.name)
} object ContravarianceTest extends App {
val myCat: Cat = Cat("Boots") def printMyCat(printer: Printer[Cat]): Unit = {
printer.print(myCat)
} val catPrinter: Printer[Cat] = new CatPrinter
val animalPrinter: Printer[Animal] = new AnimalPrinter printMyCat(catPrinter)
printMyCat(animalPrinter) // 将 Printer[Animal] 当作 Printer[Cat] 使用
}
Scala Type Parameters 2的更多相关文章
- Scala Type Parameters 1
类型参数 表现形式:在名称后面以方括号表示, Array[T] 何处使用 class 中,用于定义变量.入参.返回值 class Pair[T, S](val first: T, val second ...
- Beginning Scala study note(8) Scala Type System
1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...
- type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object
今天在进行代码检查的时候出现下面的异常: type parameters of <T>T cannot be determined; no unique maximal instance ...
- Java Error : type parameters of <T>T cannot be determined during Maven Install
遇到了一个问题如下: Caused by the combination of generics and autoboxing. 这是由于泛型和自动装箱联合使用引起的. 可以查看以下两个回答: 1 ...
- learning scala type alise
How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, ...
- learning scala repreated parameters
- Scala 深入浅出实战经典 第78讲:Type与Class实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...
- scala类型系统 type关键字
和c里的type有点像. scala里的类型,除了在定义class,trait,object时会产生类型,还可以通过type关键字来声明类型. type相当于声明一个类型别名: scala> t ...
- Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag
反射reflection是程序对自身的检查.验证甚至代码修改功能.反射可以通过它的Reify功能来实时自动构建生成静态的Scala实例如:类(class).方法(method).表达式(express ...
随机推荐
- QML官方文档:Qt Quick Controls 1和2对比
Qt Quick Controls有1和2两个版本,在程序中会看到好多1和2版本混合使用的情况. 原文:https://doc.qt.io/qt-5/qtquickcontrols2-differen ...
- Hyper-v,装XP的时候没有驱动上不了网,装这个集成服务(vmguest.iso )就可以了
Win10自带的Hyper-v,装XP的时候没有驱动上不了网,装这个集成服务(vmguest.iso )就可以了 安装后无法识别显卡及网卡设备,不能与虚拟网络通讯,设备管理器中显示三个未知设备. 在X ...
- day 67 作业
有红, 黄, 蓝三个按钮, 以及一个200X200px的矩形box, 点击不同的按钮, box的颜色会被切换为指定的颜色 <!DOCTYPE html> <html lang=&qu ...
- elastic date时区问题解决办法
之前介绍filter date插件时就谈到时区问题,但是没有说明白.最近在使用range查询时间范围内的数据时出现了数据量不一致的情况.特地了解了下ELK Stack中关于时区的问题. 问题: 使用k ...
- USB规格及速度
1. 速度对比 2. 硬件特性 USB2.0四线:5V,D-,D+,GND. USB3.0一般十线:5V,D-,D+,GND,SSTX+,SSTX-,SSRX+,SSRX-,P1_Drain,P2_D ...
- POJ 3155Hard Life(最大密度子图)
论文出处:最小割模型在信息学竞赛终的应用 #include <iostream> #include <cstdio> #include <cstring> #inc ...
- U-Boot的常用命令详解
U-Boot还提供了更加详细的命令帮助,通过help命令还可以查看每个命令的参数说明.由于开发过程的需要,有必要先把U-Boot命令的用法弄清楚.接下来,根据每一条命令的帮助信息,解释一下这些命令的功 ...
- Elasticsearch 动态修改replica配置、增删replica
1. 获取当前所有index配置 curl -XGET http://localhost:9200/_settings 2. 获取某些index的配置 curl -XGET http://localh ...
- Linux下使用cx_Oracle的一些配置
在安装完成cx_Oracle后,import cx_Oracle时报错,首先查看.bash_profile文件中环境变量配置 # .bash_profile # Get the aliases an ...
- angular 小技术点
angular 标签 ng-options ng-model ng-checked ng-true-value ng-false-value ng-if ng-src delete $location ...