• 类型关系

    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. fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC

    出现如下错误: fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires ...

  2. Fundebug录屏插件更新至0.4.0,修复BUG,优化性能

    摘要: 录屏功能更加强大,欢迎免费试用! 关于Fundebug录屏功能 Fundebug是专业的程序BUG监控服务,当线上应用出现BUG的时候,我们可以第一时间报警,帮助开发者及时发现BUG,提高De ...

  3. druid + mysql + mybatis 批量更新报错

    首先 批量更新报错 sql injection violation, multi-statement not allow 然后看了博客:https://blog.csdn.net/qq_3634595 ...

  4. yum无法下载,网关问题

    由于网关地址改变没有及时更新配置,造成无法下载 failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try h ...

  5. AI-图像基础知识-01

        目前人工智能Artificial Intelligence主要分为两大分支: 计算机视常见:Computer Vision,简称CV   CV主要是研究如何让机器看懂世界的一种技术,通过各种光 ...

  6. 【JavaScript】JavaScript基本语法&知识点

    JavaScript: 是脚本语言:是一种解释性的语言(不需要编译) 作用: 让页面有具有动态效果 组成部分: ECMAScipt(核心),包含基本语法.变量.关键字.保留字.数据类型.语句.函数等 ...

  7. hdu1801 01翻转 贪心

    题目描述: 对于给出的一个n*m的矩形,它由1和0构成,现在给你一个r*c的矩形空间可以选择,且可以选择无数次(被选中的范围内01翻转),要求问将这个01矩阵全部变成0的最少需要翻多少次,且如果无法实 ...

  8. 数据结构 - 二叉搜索树封装 C++

    二叉搜索树封装代码 #pragma once #include <iostream> using namespace std; template<class T>class T ...

  9. mysql之子查询、视图、事务及pymysql等

    数据准备 CREATE TABLE `emp` ( `id` int(0) NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL, `gender` ...

  10. LA2955 Vivian难题——梅森素数

    题意 输入 $k$(1 \leq k \leq 100)个正整数 $p_1, p_2, ..., p_k$(1 < p_i < 2{31}),找出 $k$ 个非负整数 $e_i$ 使得 $ ...