简体   繁体   English

Int 不能转换为 T

[英]Int is not convertible to T

I have the following protocol我有以下协议

public protocol NumericType {
    static func +(lhs: Self, rhs: Self) -> Self
    static func addWithOverflow(_ lhs: Self, _ rhs: Self) -> (Self, overflow: Bool)
}

Also I am extending Int to conforms to it as follows我也将 Int 扩展为符合它如下

extension Int : NumericType { }

Then I have a struct with the following definition然后我有一个具有以下定义的结构

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 does not promise that it is ExpressibleByIntegerLiteral , so there's no way to convert 0 (which is assumed to be an Int here) to T . NumericType不保证它是ExpressibleByIntegerLiteral ,因此无法将0 (此处假定为Int )转换为T Your protocol needs to provide some .zero that it knows it can initialize with (or it needs to conform to ExpressibleByIntegerLiteral ).您的协议需要提供一些.zero ,它知道它可以用它进行初始化(或者它需要符合ExpressibleByIntegerLiteral )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM