在 Scala 中所有值都有一种对应的类型

单例类型

  • 形式:value.type,返回类型 value / null

  • 场景1:链式API调用时的类型指定

    class Super {
    def m1(t: Int) = {println(t); this}
    def m2(t: Int) = {println(t); this}
    }
    // 正常打印
    new Super().m1(1).m2(2) class Child extends Super {
    def c1(t: Int) = {println(t); this}
    } // 异常 value c1 is not a member of Super
    new Child().m1(1).c1(2)

    由于 Scala 会将 this 推断为当前类(即 Super),因此无法完成链式调用

    class Super {
    // 指定返回类型为调用方的 this
    def m1(t: Int): this.type = {println(t); this}
    def m2(t: Int): this.type = {println(t); this}
    } class Child extends Super {
    def c1(t: Int) = {println(t); this}
    } // 成功打印
    new Child().m1(1).c1(2)
  • 场景2:方法中使用 object 实例作为参数

    object Foo
    class Child extends Super {
    def c1(obj: Foo.type) = {
    if (obj == Foo) println("foo")
    this
    }
    }

    Note:不可定义为 def c1(obj: Foo),因为 Foo 为单例对象,而不是类型

类型投影

  • 形式:Outer#Inner

  • 场景:内部类使用时避免类型约束

    class Outer {
    private val inners = ArrayBuffer[Inner]() class Inner (val arg1: Int) {
    val l = ArrayBuffer[Inner]()
    } def add(a: Int) = {
    val t = new Inner(a)
    inners += t
    t
    }
    } val a = new Outer
    val b = new Outer val a1 = a.add(1)
    val b1 = b.add(1)
    a1.l += b1 // error: type mismatch;

    只需要在定义内部类时指定类型投影即可解决

    // 表示适用于任何 Outer 类的 Inner 类
    val l = ArrayBuffer[Outer#Inner]()

    如果将上述例子改用 List 来实现,并不会报错,计算结果也会自动进行类型投射

路径

  • 路径中除最后一部分外,都必须是稳定状态的,如包名、objectvalthis/super/super[S]...

  • 不能包含 var 类型

    var t = new Outer()
    //...其他操作
    val i = new t.Inner // 由于 t 可能会变更,编译器无法确定其含义

    a.b.c.T 内部被翻译成类型投射 a.b.c.type#T

类型别名

  • 形式: type SomeAliasName

  • 必须定义在 classobject 内部

  • 好处: 在引用类型时可以更加简洁

    class Book {
    import scala.collection.mutable._
    // 为该类型取一个别名
    type Index = HashMap[String, Int] // 使用时不在需要重复的定义复杂的数据类型
    val map: Index = new Index()
    } new Book().map // scala.collection.mutable.HashMap[String,Int]

结构类型

  • 为抽象方法、字段、类型的定义某种规范

    def appendLines(target: { def append(str: String): Any },
    lines: Iterable[String]) {
    for (l <- lines) {
    // 此次 Scala 使用反射调用该方法
    target.append(l);
    target.append("\n")
    }
    }

    该方法第一个参数 target 即结构类型,表示使用任何包含该 append 方法的实例作为参数传入。

    由于反射的代价较大,不到万不得已不建议使用,如,有通用行为(append),却无法共享 trait

组合类型 / 交集类型

  • 形式: T1 with T2 with T3 ...

  • 当需要提供多个特质时使用,即用于约束类型

    val image = new ArrayBuffer[java.awt.Shape with java.io.Serializable]
    val rect = new Rectangle(5, 10, 20, 30)
    image += rect // 正确,Rectangle 可序列化
    image += new Area(rect) // 错误 Area 不可序列化
  • 组合类型中也可使用结构类型

    Shape with Serializable { def contains(p: Point): Boolean }

中缀类型

  • 其实只是一种语法写法,如 String Map Int 可代替 Map[String, Int]

  • 可参考数学运算中的表达方式

    type x[A, B] = (String, Int)
    // 即可使用 String x Int 来表示 (String, Int)
  • 一般中缀类型操作符都是左关联的,除了前面提到的 : 操作符,这个是右关联的,比如 List 的操作

中缀类型名称可以是任意操作符,除了 *,避免与类型定义冲突

Scala Types 1的更多相关文章

  1. Scala: Types of a higher kind

    One of the more powerful features Scala has is the ability to generically abstract across things tha ...

  2. Scala Types 2

    存在类型 形式: forSome { type ... } 或 forSome { val ... } 主要为了兼容 Java 的通配符 示例 Array[_] // 等价于 Array[T] for ...

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

  4. scala速成记录1

    选择  Learning Scala这本书,两百多页,足够薄. 安装 http://www.scala-lang.org/  下载Binary的版本.bin里边有所有操作系统下运行的可以运行的交互式s ...

  5. geotrellis使用(十九)spray-json框架介绍

    Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 spray-json简介 spray-json使用 ...

  6. Akka(33): Http:Marshalling,to Json

    Akka-http是一项系统集成工具.这主要依赖系统之间的数据交换功能.因为程序内数据表达形式与网上传输的数据格式是不相同的,所以需要对程序高级结构化的数据进行转换(marshalling or se ...

  7. 【原创】大叔问题定位分享(11)Spark中对大表子查询加limit为什么会报Broadcast超时错误

    当两个表需要join时,如果一个是大表,一个是小表,正常的map-reduce流程需要shuffle,这会导致大表数据在节点间网络传输,常见的优化方式是将小表读到内存中并广播到大表处理,避免shuff ...

  8. Spark SQL 函数全集

    org.apache.spark.sql.functions是一个Object,提供了约两百多个函数. 大部分函数与Hive的差不多. 除UDF函数,均可在spark-sql中直接使用. 经过impo ...

  9. org.apache.spark.sql.functions汇总

    测试数据: id,name,age,comment,date 1,lyy,28,"aaa bbb",20180102020325 scala> var data = spar ...

随机推荐

  1. 用react-service做状态管理,适用于react、react native

    转载自:https://blog.csdn.net/wo_shi_ma_nong/article/details/100713151 . react-service是一个非常简单的用来在react.r ...

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

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

  3. 源码安装rlwrap 0.43(为了方便使用linux下的sqlplus)

    为了linux下的sqlplus方便调用历史命令和退格,安装下rlwrap,最新版本是0.43,貌似作者已经不更新了 下载地址 https://fossies.org/linux/privat/rlw ...

  4. Odoo中的env详解

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826382.html 一:environment environment类提供了对ORM对象的封装,同时提 ...

  5. 【HttpServlet】HttpServlet类

    创建时间:6.15 HttpServlet 但在实际开发中,我们不会直接去实现Servlet接口,因为那样需要覆盖的方法太多,    我们一般创建类继承HttpServlet 实现步骤: 1)创建类继 ...

  6. 【JavaScript】案例一:使用JS完成注册页面表单校验——事件(onsubmit&onfocus&onblur)

    (一)初版:事件(onsubmit) 步骤分析: 第一步:确定事件(onsubmit)并为其绑定一个函数 第二步:书写这个函数(获取用户输入的数据<获取数据时需要在指定位置定义一个 id> ...

  7. HDU1754 && HDU1166 线段树模板题

    HDU1754 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目分析:对于给出的一个很长的区间,对其进行单点更新值和区间求最大值的操作,由于 ...

  8. java String、String.concat和StringBuilder性能对比

    看到网上有人已经做过对比,并且贴出了代码,然后我运行了之后发现跟我分析的结论差距很大.发现他的代码有个问题,UUID.randomUUID() 首次调用耗时会很高,这个耗时被计算给了String,这对 ...

  9. Kibana 学习资料

    Kibana 学习资料 网址 Kibana 官方文档 https://s0www0elastic0co.icopy.site/guide/en/kibana/current/introduction. ...

  10. 用 tomcat 部署 Jenkins

    在tomcat 下面部署 Jenkins 版本 tomcat (7.0.94) +  jdk (7) + jenkins (2.46.3) 前两次没有部署成功,应该是 jdk 版本的问题, 如果不成功 ...