swift – 为什么我会收到错误“Protocol …只能用作通用约束,因为它有Self或相关的类型要求”?

swift – 为什么我会收到错误“Protocol …只能用作通用约束,因为它有Self或相关的类型要求”?,第1张

概述我在Int上写了一个扩展名,如下所示. extension Int { func squared () -> Int { return self * self }}print(10.squared()) // works 上面的代码有效.现在我想扩展IntegerType协议,以便Int,UInt,Int64等都符合.我的代码如下. extension Int 我在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.

简单地说:

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

总结

以上是内存溢出为你收集整理的swift – 为什么我会收到错误“Protocol …只能用作通用约束,因为它有Self或相关的类型要求”?全部内容,希望文章能够帮你解决swift – 为什么我会收到错误“Protocol …只能用作通用约束,因为它有Self或相关的类型要求”?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009734.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存