• 类型关系

    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的更多相关文章

  1. Scala Type Parameters 1

    类型参数 表现形式:在名称后面以方括号表示, Array[T] 何处使用 class 中,用于定义变量.入参.返回值 class Pair[T, S](val first: T, val second ...

  2. 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 ...

  3. 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 ...

  4. Java Error : type parameters of <T>T cannot be determined during Maven Install

    遇到了一个问题如下: Caused by the combination of generics and autoboxing. 这是由于泛型和自动装箱联合使用引起的. 可以查看以下两个回答:   1 ...

  5. learning scala type alise

    How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, ...

  6. learning scala repreated parameters

  7. Scala 深入浅出实战经典 第78讲:Type与Class实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

  8. scala类型系统 type关键字

    和c里的type有点像. scala里的类型,除了在定义class,trait,object时会产生类型,还可以通过type关键字来声明类型. type相当于声明一个类型别名: scala> t ...

  9. Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag

    反射reflection是程序对自身的检查.验证甚至代码修改功能.反射可以通过它的Reify功能来实时自动构建生成静态的Scala实例如:类(class).方法(method).表达式(express ...

随机推荐

  1. error: undefined reference to `vtable for

    出现如下错误: 解决办法 当类中加入Q_OBJECT,需要手动删除中间文件,再构建

  2. 基于Spring Boot的注解驱动式公众号极速开发框架FastBootWeixin

    本框架基于Spring Boot实现,使用注解完成快速开发,可以快速的完成一个微信公众号,重新定义公众号开发. 在使用本框架前建议对微信公众号开发文档有所了解,不过在不了解公众号文档的情况下使用本框架 ...

  3. logger(一)slf4j简介及其实现原理

    一.slf4j简介 slf4j(Simple logging facade for Java)是对所有日志框架制定的一种规范.标准.接口,并不是一个框架的具体的实现,因为接口并不能独立使用,需要和具体 ...

  4. rac启动维护笔记

    Ohasd.bin将产生4个代理启动相关的资源 (1)    oraagent:负责ora.asm.ora.evmd.ora.gipcd.ora.gpnpd.ora.mdnsd的启动和管理 (2)   ...

  5. ETHINK组件取值手册

    Ethink组件取值手册 一.取值 Sql查询配置中取值方式:所有可以对外过滤的组件都可以用id.output取值 就是取组件setOutput()里输出的值 ,具体分为以下两种: 1)$p{OBJ_ ...

  6. 将积累多年的java学习资料,pdf文档给大家分享一下,比如《大话设计模式》《算法》《Linux私房菜》等等

    话不多说,资料目录如下,需要的拿走,下载的同时别忘了点下推荐. 需要的点下面链接,到小编网盘进行下载: 2-大话设计模式.pdf 链接:https://pan.baidu.com/s/1b05puNO ...

  7. passwd修改密码失败,报鉴定令牌操作错误

    出现这个情况,从四个方面来分析: 1./usr/bin/passwd 的权限中没有添加s即SUID特殊权限 即:-rwxr-xr-x. 1 root root 27000 8月  22 2010 /u ...

  8. CentOS 7 安装配置分布式文件系统 FastDFS 5.0.5

    前言 项目中用到文件服务器,有朋友推荐用FastDFS,所以就了解学习了一番,感觉确实颇为强大,在此再次感谢淘宝资深架构师余庆大神开源了如此优秀的轻量级分布式文件系统,本篇文章就记录一下FastDFS ...

  9. IDEA中如何导入一个maven项目并配置相关设置

    导入一个maven项目参照如下链接 https://jingyan.baidu.com/article/b0b63dbf0c0ac04a49307078.html 要想启动这个导入的项目目前我所接触到 ...

  10. 谷歌学术出现We're sorry解决办法

    出现这个的原因应该是同ip段的或者就是这个ip曾经是个google的黑名单ip,因为恶意爬取谷歌学术了.解决办法就是申请Hurricane Electric Free IPv6 Tunnel Brok ...