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 ...
随机推荐
- WampServer出现You don’t have permission to access/on this server提示
WampServer出现You don’t have permission to access/on this server提示 本地搭建WampServer,输入http://127.0.0.1访问 ...
- Keepalived与MySQL互为主从自动切换配置
为解决Mysql数据库单点问题,实现两台MySQL数据库互为主备,双向replication.当一Master出现问题,则将Slave切换为Master继续工作. 环境说明 系统版本:CentOS L ...
- mysql后台线程详解
1.mysql后台线程 mysql后台线程主要用于维持服务器的正常运行和完成用户提交的任务,主要包括:master thread,read thread,write thread,redo log t ...
- Elasticasearch Web管理工具-Cerebro
cerebro是一个使用Scala,Play Framework,AngularJS和Bootstrap构建的开源(MIT许可)elasticsearch web管理工具.需要Java 1.8或更高版 ...
- TCP链接异常断开后,对端仍然ESTABLISH
双方建立TCP链接,其中一方拔掉网线,另一端依然是ESTABLISHED,那么要过多长时间才会发觉链接被断开了呢? [root@node1 ~]# sysctl -a |grep keepalive ...
- mds0: Many clients (191) failing to respond to cache pressure
目录 背景 后续的努力 临时的解决办法 cephfs时我们产品依赖的主要分布式操作系统,但似乎很不给面子,压力测试的时候经常出问题. 背景 集群环境出现的问题: mds0: Many clients ...
- Flask拾遗总汇1
目录 1.flask的路由分发方式 2.请求响应相关 3.flask配置文件拾遗(config) 4.路由系统参数配置 4.1 可传入参数: 4.2 常用路由系统有以上五 5.反向生成URL: url ...
- CSP 201903-2 24点
这是上一次考csp时遇到的一道简单的问题,但是当时太菜了没有写出来. 问题描述: 直接上图 解决思路: 标准的表达式求解,可以用符号栈和数值栈来存放运算符和数值,需要注意的是从左到右扫描的时候 遇到 ...
- 常用.Net 6.0 新特性
1.nameof表达式.Nameof表达式可以直接返回对象定义的名称,比如参数.枚举.变量. 控件.属性等.可以大大减少硬编码的使用,提高程序灵活性. }, 2.字符串嵌入值($). MsgBox.S ...
- ZROI 暑期高端峰会 A班 Day4 树上数据结构
FBI Warning:本文含有大量人类的本质之一. 你经历过绝望吗? [ZJOI2007]捉迷藏 询问树上最远黑点对. 动态边分治可以比点分治少一个 \(\log\). bzoj3730 咕了. [ ...