`

scala中的协变,逆变

 
阅读更多
trait Queue[+T] { ... }

Prefixing a formal type parameter with a + indicates that subtyping is covariant (flexible) in that parameter. By adding this single character, you are telling Scala that you want Queue[String], for example, to be considered a subtype of Queue[AnyRef]. The compiler will check that Queue is defined in a way that this subtyping is sound.Besides +, there is also a prefix -, which indicates contravariant subtyping.
If Queue were defined like this:
trait Queue[-T] { ... }

then if T is a subtype of type S, this would imply that Queue[S] is a subtype of Queue[T] (which in the case of queues would be rather surprising!).Whether a type parameter is covariant, contravariant, or nonvariant is called the parameter’s variance . The + and - symbols you can place next to type parameters are called variance annotations.
分享到:
评论

相关推荐

    scala课件.zip

    Scala概述.pptx 变量.pptx 运算符.pptx 程序流程控制.pptx 函数式编程基础.pptx 面向对象编程(基础部分).pptx ...泛型 上下界 视图界定 上下文界定 协变逆变不变.pptx AKKA.pptx 韩顺平_Scala语言核心编程.pdf

    头歌Scala中集合的使用

    Scala中集合的使用 大学生 1. List 列表的使用 2. Set 集合的使用 3.Map 映射的使用 4. 元组的使用

    scala编程中文pdf

    scala编程 33章 中文pdf Scala编程实战 目录 第1章字符串. 11 第2章数值39 第3章控制结构.60 第4章类和属性.103 第5章方法147 第6章对象170 第7章包和导入.190 第8章特质200 第9章函数式编程214 第10 章集合242 第...

    Scala_中文学习资料_含Scala_2.7.6_API.rar

    Scala_中文学习资料_含Scala_2.7.6_API.rar。Scala_中文学习资料_含Scala_2.7.6_API.rar。

    scala 中文学习 入门与进阶

    scala.chm 中文学习 入门与进阶, 以及用到的构建工具与测试工具

    Scala函数式编程

    2 无副作用的函数式编程,同时也解释了为什么在scala中,val和var的区分为什么那么重要。 3 在做习题的过程中,尤其是在做类型推导的过程中,对原来oop,命令式编程向函数式编程转变有很大作用;而且简洁的语法,...

    scalgos, Scala 中的算法.zip

    scalgos, Scala 中的算法 目标学习 ScalaText 实现惯用函数 Scala 中常用算法没有外部依赖项( 除了测试的specs2 )良好的测试和文档建筑安装 git,Scala 和 sbt: brew install git s

    scala编程中文

    scala编程中文版本(33章全)。大数据必备参考书箱。非常适合初学者的一本书,这本书的作者就是scala语言的创始人

    programing in scala中文版的scala教程哦

    programing in scala中文版的scala教程哦。

    scala + mybatis 数据库查询

    这是一个简单实用的scala集成mybatis数据库查询代码,简单明了

    scala sdk scala-2.12.3

    scala-2.12.3 scala-2.12.3 scala-2.12.3 scala-2.12.3

    scala-xml_2.11-1.0.1-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.1.jar; 赠送原API文档:scala-xml_2.11-1.0.1-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.1-sources...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala 语法参考中文pdf

    Scala 语言规范 2.7版 + Scala 编程 两本中文pdf的电子文档。

    scala-reflect-2.12.10-API文档-中文版.zip

    赠送jar包:scala-reflect-2.12.10.jar; 赠送原API文档:scala-reflect-2.12.10-javadoc.jar; 赠送源代码:scala-reflect-2.12.10-...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala 2.13.3 API 文档 Html离线版 scala-api.rar

    最新制作 scala 2.13.3 API 文档 Html离线版 Scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性。 Scala 运行在 Java 虚拟机上,并兼容现有的 Java 程序。

    快学Scala.中文完整版

    中文版scala。《快学Scala》是一门以Java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程语言的特性结合在一起的编程语言。你可以使用Scala编写出更加精简的程序,同时充分利用并发的威力。由于Scala运行于JVM...

    scala-reflect-2.12.7-API文档-中文版.zip

    赠送jar包:scala-reflect-2.12.7.jar; 赠送原API文档:scala-reflect-2.12.7-javadoc.jar; 赠送源代码:scala-reflect-2.12.7-sources...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala-compiler-2.12.7-API文档-中文版.zip

    赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala-sbt-scala编译工具

    scala 编译工具 sbt 安装包。 Little or no configuration required for simple projects Scala-based build definition that can use the full flexibility of Scala code Accurate incremental recompilation ...

Global site tag (gtag.js) - Google Analytics