什么是“协议......只能用作通用约束,因为它具有 Self 或相关的类型要求"?意思? [英] What does "Protocol ... can only be used as a generic constraint because it has Self or associated type requirements" mean?

查看:34
本文介绍了什么是“协议......只能用作通用约束,因为它具有 Self 或相关的类型要求"?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个字典(实际上是一个 HashSet)Swift,但它给了我标题中的错误:

I am trying to create a Dictionary (actually a HashSet) keyed on a custom protocol in Swift, but it is giving me the error in the title:

Protocol 'myProtocol' 只能用作泛型约束,因为它具有 Self 或关联类型要求

Protocol 'myProtocol' can only be used as a generic constraint because it has Self or associated type requirements

我无法理解它.

protocol Observing: Hashable { }

var observers = HashSet<Observing>()

推荐答案

Protocol Observing 继承自协议 Hashable,而后者又继承自协议 Equatable.协议 Equatable 有以下要求:

Protocol Observing inherits from protocol Hashable, which in turn inherits from protocol Equatable. Protocol Equatable has the following requirement:

func ==(lhs: Self, rhs: Self) -> Bool

并且一个包含 Self 的协议不能在任何地方使用,除非在类型约束中.

And a protocol that contains Self somewhere inside it cannot be used anywhere except in a type constraint.

这里是类似的问题.

这篇关于什么是“协议......只能用作通用约束,因为它具有 Self 或相关的类型要求"?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆