1
有沒有什麼方法可以在Swift中描述IntegerType
有一個max
屬性? (類似於go中的隱式接口)Swift隱式協議
沒有協議來描述max
屬性,即使我創建了一個,IntegerType
也沒有明確實現它。
所以基本上我正在尋找類似:
class Test<T: IntegerType where ?> { // <- ? = something like 'has a "max: Self"' property
}
let t = Test<UInt8>()
或也許是這樣的:
implicit protocol IntegerTypeWithMax: IntegerType {
static var max: Self { get }
}
class Test<T: IntegerTypeWithMax> {
}
let t = Test<UInt8>()
只需刪除「隱含」然後編譯... –
@MartinR,它編譯,但它沒有做我想做的事情,因爲'IntegerType'沒有實現'IntegerTypeWithMax',所以我不能使用任何'IntegerType'(例如'UInt8')實例化一個類作爲參數。 – rid
編譯器不知道符合'IntegerType'的所有類型是否具有'max'屬性。你必須告訴他如何'擴展UInt8:IntegerTypeWithMax {}'。 (Swift 2中的新協議擴展可能有更好的方法。) –