swift - 为什么我会收到此警告以在符合默认实现的协议的类型上实现“hash(into:)”
问题描述
下面的代码显示了警告:
❯ swiftc demo.swift [13:06:57]
Swift.RawRepresentable:2:27: warning: 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'PlaybackSpeed' to 'Hashable' by implementing 'hash(into:)' instead
@inlinable public var hashValue: Int { get }
枚举 PlaybackSpeed 应该具有默认实现的函数resetSettings()
& hash(into:)
,因此不应生成此警告。
我的理解是错误的还是编译器错误?
protocol Settingsable {
func resetSettings()
}
protocol SettingsSelectable: Hashable, Settingsable {
var display: String { get }
}
extension SettingsSelectable {
func hash(into hasher: inout Hasher) {
hasher.combine(display)
}
}
extension SettingsSelectable {
func resetSettings() {
print("These have been reset")
}
}
enum PlaybackSpeed: Int, SettingsSelectable {
case half
case standard
case onePointFive
case double
var display: String {
switch self {
case .half:
return "0.5x"
case .standard:
return "1.0x"
case .onePointFive:
return "1.5x"
case .double:
return "2.0x"
}
}
}
解决方案
此代码有两个默认实现hash(into:)
。一个来自 Int,一个来自 SettingSelectable。
我不确定这是否是定义的行为。我的期望是使用 Int 实现并忽略 SettingsSelectable 扩展。无论如何,诊断不是很好。我建议为此打开一个缺陷。
Int
您可以通过删除或通过显式实现来修复此错误,hash(into:)
以便清楚您的意思。或者您可以创建另一层协议:
protocol SettingsSelectableBase: Hashable, Settingsable {
var display: String { get }
}
protocol SettingsSelectable: SettingsSelectableBase {}
// Only give the default to things that ask for it, not to Base conformers
extension SettingsSelectable {
func hash(into hasher: inout Hasher) {
hasher.combine(display)
}
}
extension SettingsSelectableBase {
func resetSettings() {
print("These have been reset")
}
}
enum PlaybackSpeed: Int, SettingsSelectableBase { ... }
推荐阅读
- python - 在二维中绘制具有许多权重的线性回归分离器
- javascript - d3.js csv 数据过滤器包含一个单词
- android - 未调用 Firebase 的 ChildEventListener 方法
- python-3.x - 将视频与 ffmpeg 连接时,AWS Lambda 上的 FileNotFoundError
- r - 如何在选项卡中将两个 uiOutput 提供给 renderUI
- pattern-matching - 如何将参数限制为仅变体类型的一个变体
- ruby-on-rails - 如何使用 RoR 实施 ABAC 策略
- mysql - 在 INSERT INTO 问题中使用两个 SELECT FROM 语法
- c - 代码块上的 Openmp
- python-3.x - Python/Kivy - 将变量从一个类发送到另一个类