教材:快學(xué)Scala
chapter 18. 高級類型 Advanced Types
18.1 單例類型 Singleton Types
*what the fuck*
對于引用p,p.type是一個單例類型,值只能為p或者null
對于單例對象P,P.type是一個單例類型,值只能為P或者null
class MyPair[S, T](val first: S, val second: T)
val p = new MyPair(1, 2.0)
val pt: p.type = p // 值只能為p或者null
println(pt.first, pt.second) // (1,2.0) p和pt一模一樣
object P { val first = 1; val second = 2 }
val Pt: P.type = P // 值只能為P或者null
println(P.first, P.second) // (1,2.0)
println(Pt.first, Pt.second) // (1,2.0) P和Pt一模一樣
object Title // 單例對象
object Desc // 單例對象
class Document {
private var useNextArgAs: Any = null
private var title: String = null
private var desc: String = null
def set[T](obj: T): this.type = { useNextArgAs = obj; this } // this.type 類型用于有繼承關(guān)系的鏈?zhǔn)秸{(diào)用
def to(arg: String) = useNextArgAs match { // useNextArgAs 匹配不同的單例類型
case tt: Title.type => title = arg
case dt: Desc.type => desc = arg
case _ => arg
}
def p = println("title = " + title + "desc = " + desc)
}
val book = new Document
book set Title to "Scala for the impatient" // "fluent interfaces"
book set Desc to "a book full of joy when reading"
book p
18.2 類型投影 Type Projections
嵌套類
class Network {
class Memeber {...}
...
}
val chatter = new Network
val myFace = new Network
每個Network實例的Member是不同的類,即chatter.Member和myface.Member是不同的類。
類型投影:Network#Member 表示"任何Network的Member"
18.4 類型別名 Type Aliases
-
類型別名
type Index = HashMap[String, (Int, Int)]
類型別名必須被嵌套在類或?qū)ο笾?,不能出現(xiàn)在Scala文件的頂層。
18.5 結(jié)構(gòu)類型 Structural Types
- 描述一個變量必須有哪些抽象方法/字段/替他規(guī)格說明
def appendLines(target: {def append(str: String): Any}, ...) {...}
target可以為任何有append方法的實例,比定義一個Appendable特質(zhì)要靈活,但是使用反射機(jī)制調(diào)用target.append,反射調(diào)用的開銷要大得多(much more expensive) - 鴨子類型(duck typing):結(jié)構(gòu)類型與js中的鴨子類型很類似,即:obj不需要一定是Duck類的實例,運行時只要obj.quack()在被調(diào)用那一刻檢查到有quack方法就能調(diào)用成功。
18.6 復(fù)合類型 Compound Types
T1 with T2 with T3 也叫交集類型
trait ImageShape extends Shape with Serializable 的意思是
trait ImageShape extends (Shape with Serializable)
18.7 中置類型 Infix Types
帶有兩個類型參數(shù)的類型T1 A T2
18.8 存在類型 Existential Types
為了與Java的類型通配符兼容
-
存在類型 類型表達(dá)式后面跟上forsome {...}
Array[_ <: JComponent]等價于Array[T] forsome { type T <: JComponent }
Array[_]等價于Array[T] forSome { type T }
Map[_, _]等價于Map[T, U] forSome { type T; type U }
18.9 方法類型 method type
Scala編譯器內(nèi)部使用的類型,方法類型表示為(T1, ..., Tn)T 不帶=>
def square(x: Int) = x * x // 方法,類型為(x: Int)Int
val triple = (x: Int) => 3 * x // 函數(shù),類型為Int => Int = <function1>
18.10 自身類型 Self Types
用于使trait可以要求混入它的類擴(kuò)展自另一個實例
用法 this: 類型 =>
自身類型不會自動繼承,在子類中需要重復(fù)自身類型的聲明