Scala Types 1
在 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来实现,并不会报错,计算结果也会自动进行类型投射
路径
路径中除最后一部分外,都必须是稳定状态的,如包名、
object、val、this/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必须定义在
class或object内部好处: 在引用类型时可以更加简洁
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的更多相关文章
- Scala: Types of a higher kind
One of the more powerful features Scala has is the ability to generically abstract across things tha ...
- Scala Types 2
存在类型 形式: forSome { type ... } 或 forSome { val ... } 主要为了兼容 Java 的通配符 示例 Array[_] // 等价于 Array[T] for ...
- 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 ...
- scala速成记录1
选择 Learning Scala这本书,两百多页,足够薄. 安装 http://www.scala-lang.org/ 下载Binary的版本.bin里边有所有操作系统下运行的可以运行的交互式s ...
- geotrellis使用(十九)spray-json框架介绍
Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 spray-json简介 spray-json使用 ...
- Akka(33): Http:Marshalling,to Json
Akka-http是一项系统集成工具.这主要依赖系统之间的数据交换功能.因为程序内数据表达形式与网上传输的数据格式是不相同的,所以需要对程序高级结构化的数据进行转换(marshalling or se ...
- 【原创】大叔问题定位分享(11)Spark中对大表子查询加limit为什么会报Broadcast超时错误
当两个表需要join时,如果一个是大表,一个是小表,正常的map-reduce流程需要shuffle,这会导致大表数据在节点间网络传输,常见的优化方式是将小表读到内存中并广播到大表处理,避免shuff ...
- Spark SQL 函数全集
org.apache.spark.sql.functions是一个Object,提供了约两百多个函数. 大部分函数与Hive的差不多. 除UDF函数,均可在spark-sql中直接使用. 经过impo ...
- org.apache.spark.sql.functions汇总
测试数据: id,name,age,comment,date 1,lyy,28,"aaa bbb",20180102020325 scala> var data = spar ...
随机推荐
- 高性能的编程IO与NIO阻塞分析
1.什么是阻塞,什么是非阻塞? 阻塞:结果返回之前,线程一直被挂起. 非阻塞:做一件事,尝试去做 2.传统IO模型 socket编程:
- Html引入百度富文本编辑器ueditor及自定义工具栏
在日常工作用,肯定有用到富文本编辑器的时候,富文本编辑器功能强大使用方便,我用的是百度富文本编辑器,首先需要下载好百度编辑器的demo, 然后创建ueditor.html文件,引入百度编辑器,然后在h ...
- 『计算机视觉』imgaug图像增强库中部分API简介
https://github.com/aleju/imgaug 介绍一下官方demo中用到的几个变换,工程README.md已经给出了API简介,个人觉得不好理解,特此单独记录一下: import n ...
- 第05节-BLE协议物理层(PHY)
对于软件的人来说,物理层关注的内容会少一点.在前面的博客中,我们以快递员类比物理层,对于快递员来说,道路千万条,这条不通换另外一条.对于物理层来说也是一样的,它有很多频率,这个频率有冲突了,将会切到另 ...
- update mantis_bug_table
update mantis_bug_table set mantis_bug_table.original_due_date=mantis_bug_table.due_date ,) ,);
- python将数组写入文件
import numpy as npdata = np.array([[1,2], [3,4]]) np.savetxt('out.txt', data, fmt="%d") #保 ...
- Python面向对象 | isinstance和issubclass
isinstance(a,b):判断a是否是b类(或者b类的基类)实例化的对象 class A: pass class B(A): pass obj = B() print(isinstance(ob ...
- Pandas | 07 函数应用
要将自定义或其他库的函数应用于Pandas对象,有三个重要的方法,下面来讨论如何使用这些方法.使用适当的方法取决于函数应用于哪个层面(DataFrame,行或列或元素). 表合理函数应用:pipe() ...
- NetworkManager需要与不需要
需要: 1.nmcli配置网络 不需要: 1. ifcfg-eth1:0(网卡的子接口配置文件)生效
- 关于System.FormatException异常
什么是FormatException 参数格式无效或复合格式字符串不正确时引发的异常. 继承 Object Exception SystemException FormatException 详细说明 ...