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 ...
随机推荐
- Vue-Cli 指南
构建项目: vue create 文件夹名称 运行项目:(README文件查询) npm run serve
- jvm运行时数据区之程序计数器
什么是程序计数器? 程序计数器是一块 较小 的内存空间,它可以看做是当前线程所执行的字节码的 行号指示器 :在虚拟机的概念模型里(仅仅是概念模型,各种虚拟机可能会通过一些更高效的方式去实现),字节码解 ...
- 5种处理Vue异常的方法
原文: Handling Errors in Vue.js 译者: Fundebug 本文采用意译,版权归原作者所有 去年一整年,我都在使用最爱的-Vue.js- 来做项目.最近突然意识到,我竟然从来 ...
- Cloud Alert 实现告警智能降噪,成功规避告警风暴
# 前言 睿象云前段时间发表了一篇[< Zabbix 实现电话.邮件.微信告警通知的实践分享>](https://www.toutiao.com/i6734876723126469127/ ...
- docker上启动mysql镜像,mysq中记录乱码解决方法
在docker上启动一个mysql, 1. docker pull mysql 2. docker run --name mysql_dev -p 3306:3306 -e MYSQL_ROOT_PA ...
- MySQL多实例安装、配置、启动(四)
一.规划信息 系统信息: cat /etc/redhat-release CentOS Linux release (Core) # uname -r -.el7.x86_64 数据库规划 PORT: ...
- 绘制指引线的JS库leader-line
前言 之前看到一篇推荐Magi这个搜索引擎的新闻,对于这个搜索引擎是否好用咱们不予置评,但是我在这个搜索引擎上面发现了一个好玩的前端功能. 如上图,将鼠标浮动到学习来源上时,会展示一堆指引线. 本博客 ...
- 08-numpy-笔记-sum
求和: axis = 0 按列求和 axis = 1 按行求和 >>> import numpy as np >>> a = np.mat([[1,2,3],[4, ...
- 掌握Visual Studio断点
我知道你现在在想什么.断点?真的吗?关于断点有什么需要掌握的?你按F9然后停在代码行.如果你是这么想的,这篇文章是给你的:继续读 Visual Studio提供了一组相当丰富的断点类型和操作,可以使您 ...
- 1-PLC基础入门系列(PLC介绍,连接下载说明)
阅读这节文章之前请先阅读这一篇 https://www.cnblogs.com/yangfengwu/p/7681702.html 首先明确一点,PLC就是用单片机做的,后面我会给大家演示用我自己的 ...