加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 为什么我会收到错误“Protocol …只能用作通用约束,因

发布时间:2020-12-14 04:48:20 所属栏目:百科 来源:网络整理
导读:我在Int上写了一个扩展名,如下所示. extension Int { func squared () - Int { return self * self }}print(10.squared()) // works 上面的代码有效.现在我想扩展IntegerType协议,以便Int,UInt,Int64等都符合.我的代码如下. extension IntegerType { func sq
我在Int上写了一个扩展名,如下所示.

extension Int {
    func squared () -> Int {
        return self * self
    }
}

print(10.squared()) // works

上面的代码有效.现在我想扩展IntegerType协议,以便Int,UInt,Int64等都符合.我的代码如下.

extension IntegerType {

    func squared () -> IntegerType { // this line creates error

        return self * self

    }
}

我收到错误:

Protocol ‘IntegerType’ can only be used as a generic constraint because it has Self or associated type requirements

我已经看过this问题及其视频& this问题,仍然无法理解.我只知道有一些associatedType,在这种情况下是Self但无法连接点.我觉得我对Generics主题缺乏了解也是一个原因……

有人可以详细说明这个主题,为什么扩展会产生错误?

解决方法

函数返回类型只能是具体的类型.

关键是Type.任何完全定义的结构,类或协议都是纯类型.但是,当协议或结构依赖于另一个通用类型占位符(如T)时,则这是部分类型.

Type是编译器必须分配特定内存的数据结构.

所以像这样:

设a = Array< T>()或者让b = T不足以让编译器在编译时推导出来.

因此,这不会奏效.

extension IntegerType {

    func squared () -> IntegerType { // this line creates error

        return self * self

    }
}

这里,IntegerType是部分类型.它是一种通用协议,只有在符合要求时才能知道确切的类型.与Array类似.数组本身不是一种类型.它是一个通用容器.只有当有人用Array()或Array()创建它时…它才有类型.

同样的事发生在你身上.

public protocol IntegerType : _IntegerType,RandomAccessIndexType {

然后,

public protocol RandomAccessIndexType : BidirectionalIndexType,Strideable,_RandomAccessAmbiguity {
@warn_unused_result
    public func advancedBy(n: Self.Distance) -> Self

然后,

public protocol _RandomAccessAmbiguity {
    associatedtype Distance : _SignedIntegerType = Int
   }

因此,由于RandomAccessIndexType具有自我要求含义,除非有人符合它,否则Self是未知的占位符.它是部分类型.

由于IntegerType符合RandomAccessIndexType和_RandomAccessAmbuiguity,它也需要距离关联类型.

因此你也不能这样做

let a: IntegerType = 12

同样,IntegerType需要知道Self和Distance(associatedType).

然而,Int提供了类似的细节

public struct Int : SignedIntegerType,Comparable,Equatable {
    /// A type that can represent the number of steps between pairs of
    /// values.
    public typealias Distance = Int

因此你可以做到这一点

let a:Int = 10

因为它为其他对应物提供了Self for SignedIntegerType和Distance.

简单地说:

在具体类型可以使用的情况下不能使用部分类型.部分类型适用于其他泛型并限制它们.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读