• 类型关系

    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. 剑指:包含min函数的栈(min栈)

    题目描述 设计一个支持 push,pop,top 等操作并且可以在 O(1) 时间内检索出最小元素的堆栈. push(x)–将元素x插入栈中 pop()–移除栈顶元素 top()–得到栈顶元素 get ...

  2. java-log4j配置

    引入依赖: <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId ...

  3. 利用 FluentScheduler 启动定时器计划任务

    FluentScheduler 是什么? Automated job scheduler with fluent interface. 这是作者在 Github 上的介绍,就是一个定时任务管理器.在 ...

  4. c++ c的拓展

    C++对c的拓展之, 引用和const关键字 bool类型关键字 C++中的布尔类型 C++在C语言的基本类型系统之上增加了bool C++中的bool可取的值只有true和false 理论上bool ...

  5. django 基础进 COOKIE

    1 cookie session auth cookie概念:针对每一个服务器,保存在客户端浏览器的一个key-value结构数据,可以理解成一个字典结构 cookie语法:              ...

  6. 《Exception团队》第三次作业:团队项目的原型设计

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 学习原型设计过程以及工具的使用 二.原型设计细 ...

  7. swagger 配置- ssm

    swagger 配置 - ssm swagger 是一个用来看接口的工具,具体效果如下,这里用的是swagger2 1.porm.xml <dependency> <groupId& ...

  8. python将数组写入文件

    import numpy as npdata = np.array([[1,2], [3,4]]) np.savetxt('out.txt', data, fmt="%d") #保 ...

  9. hdu5285-wyh2000 and pupil-(染色法二分图判定)

    http://acm.hdu.edu.cn/showproblem.php?pid=5285 题意:把互不认识的人分到两个组,第一组人数尽可能多. 题解:把互不认识的人连起来,当作二分图,二分图可能有 ...

  10. 【Linux】netstat命令

    https://www.cnblogs.com/ftl1012/p/netstat.html这个讲的不错 https://www.linuxprobe.com/netstat-common-metho ...