[英]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.