存在型別
-
形式:
forSome { type ... }或forSome { val ... } -
主要為了兼容 Java 的通配符
-
示例
Array[_] // 等價于 Array[T] forSome { type T} Map[_, _] // 等價于 Map[T, U] forSome { type T; type U <: T}
型別系統
| 型別 | 語法 |
|---|---|
| Class/Trait | class C, trait T |
| 元組 | (T1, T2...) |
| 函式 | (P1, P2...) => T |
| 注解 | T @A |
| 引數型別 | A[T1, T2...] |
| 單例型別 | value.type |
| 型別投射 | O#I |
| 組合型別 | T1 with T2 ... |
| 中綴型別 | T1 A T2 |
| 存在型別 | T forSome { type/val... } |
以上型別可在撰寫程式時定義,Scala 也有少量的型別在編譯器內部使用
def square(x: Int) = x * x
// REPL 中回傳的型別為
// square(x: Int) Int
// 省略的方法定義的 =>
自身型別 self type
-
形式:
this: Type => -
用于限制
trait只能被混編于指定型別的子類中trait T1 { def m1()} trait T2 extends T1 { this: Super1 with Super2 => def m1() { methodInSuper() } } // 使用時只能在 Super1,Super2 的子類中混編 with T2 -
引入的問題:自身型別不會自動繼承,必須在子類中重復定義
trait T3 extends T2 { this: Super1 with Super2 => // 必須重復定義 }
依賴注入
-
通過
trait和 自身型別 實作簡單的以來注入- 需要將所有的依賴都組合起來
trait Logger { def log(msg: String) } trait Auth { this: Logger => def login(id: String, password: String): Boolean } trait App { this: Logger with Auth => // ... } object MyApp extends App with FileLogger("test.log") with MockAuth("users.txt") -
蛋糕模式 (
cake pattern) 實作依賴注入- 依賴的組件使用自身型別來表示
trait描述服務介面val定義需要實體化的服務- 層級化組合各個組件,在一個整體中注入需要的組件
// 定義組件1 trait LoggerComponent { // 描述介面 trait Logger { ... } // 需要實體化的服務 val logger: Logger // 介面具體實作 class FileLogger(file: String) extends Logger { ... } ... } // 定義組件2 trait AuthComponent { // 自身型別限定混編使用的型別 this: LoggerComponent => // Gives access to logger // 定義服務介面 trait Auth { ... } // 需要實體化的服務 val auth: Auth // 介面具體實作 class MockAuth(file: String) extends Auth { ... } ... } // 所有的依賴都集中在一處進行配置/注入 object AppComponents extends LoggerComponent with AuthComponent { // 實體化服務/注入 val logger = new FileLogger("test.log") val auth = new MockAuth("users.txt") }Scala編程的蛋糕模式和依賴注入
抽象型別
-
形式:
type Name -
在
class或trait中定義 -
場景:具體型別需要在子類中確定
trait Reader { type Contents def read(fileName: String): Contents } // 子類實作是具體確定型別 class StringReader extends Reader { type Contents = String def read(fileName: String) = ... } class ImageReader extends Reader { type Contents = BufferedImage def read(fileName: String) = ... } -
抽象型別、型別引數的使用選擇
- 在類實體化時需要具體確認型別的場景使用型別引數,如
HashMap[String, Int] - 期望子類提供具體型別的場景使用抽象型別,如上例中的
Reader
- 在類實體化時需要具體確認型別的場景使用型別引數,如
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/134879.html
標籤:Scala
上一篇:R語言入門:向量的運算
