[英]Int is not convertible to T
我有以下協議
public protocol NumericType {
static func +(lhs: Self, rhs: Self) -> Self
static func addWithOverflow(_ lhs: Self, _ rhs: Self) -> (Self, overflow: Bool)
}
我也將 Int 擴展為符合它如下
extension Int : NumericType { }
然后我有一個具有以下定義的結構
struct State<T:NumericType> {
let current : T
init(current : T) {
self.current = current
}
static func initial() -> State<T> {
return State(current: 0) // Int is not convertible to T
}
}
NumericType
不保證它是ExpressibleByIntegerLiteral
,因此無法將0
(此處假定為Int
)轉換為T
。 您的協議需要提供一些.zero
,它知道它可以用它進行初始化(或者它需要符合ExpressibleByIntegerLiteral
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.