ios - Type 'Any' does not conform to protocol 'Equatable' when trying to conform to an equatable generic Set
问题描述
I'm trying to create a base struct that supports a set of types (Int, Float, maybe string, possibly sometimes Vec4 matrix). I've built a base struct and conformed it to Hashable, made the type Equatable, but now I need to use this new struct in a Set.
This line:
struct AdjustmentSet: Set<BaseAdjustment<Any>>
where I declare the struct for the Set, is throwing
Type 'Any' does not conform to protocol 'Equatable'
Which of course is obvious in and of itself, but I don't know how to actually define a type here that works (I tried T: Equatable
, Equatable
, and nothing works).
struct BaseAdjustment<T: Equatable>: Hashable {
var hashValue: Int {
return displayName.hashValue
}
static func == (lhs: BaseAdjustment<T>, rhs: BaseAdjustment<T>) -> Bool {
return lhs.displayName == rhs.displayName &&
lhs.value == rhs.value &&
lhs.isApplied == rhs.isApplied
}
var min: T?
var max: T?
var displayName: String = ""
var value: T?
var defaultValue: T?
var isApplied: Bool = true
init(min: T, max: T, displayName: String, defaultValue: T?) {
self.min = min
self.max = max
self.displayName = displayName
self.defaultValue = defaultValue
self.value = defaultValue
self.isApplied = true
}
}
struct AdjustmentSet: Set<BaseAdjustment<Any>> {
static var brightness = BaseAdjustment<CGFloat>(min: 0, max: 1, displayName: "Brightness", defaultValue: 0)
static var exposure = BaseAdjustment<CGFloat>(min: 0, max: 1, displayName: "Exposure", defaultValue: 0)
static var contrast = BaseAdjustment<CGFloat>(min: 0, max: 1, displayName: "Contrast", defaultValue: 0)
}
Addendum:
I'm hopeful that with the introduction of Opaque Types
in Swift 5, we can do away with this complexity by defining the set of type some BaseAdjustment
.
解决方案
AdjustmentSet
has to itself be generic:
struct AdjustmentSet<T: Equatable>: Set<BaseAdjustment<T>> {
}
But then of course, Set
is a struct, not a class or protocol, so it can't be subtyped like this.
推荐阅读
- javascript - 在装饰器中将 Typescript Typing 添加到函数原型
- macos - Applescript单击状态栏应用程序的菜单项
- python - 将熊猫数据框中的列转换为日期时间
- node.js - 如何在 MongoDB/Mongoose 中动态添加模式
- ruby - 带有可变数量参数的 Ruby 调用系统或 exec
- python - 具有多个变量的 Python 输入验证...简化了吗?
- angular - 无法获取 Angular ng serve 命令来建立 https 上下文
- php - PHP 7 Imagick writeimage() 路径问题
- javascript - 如何解决 InvalidValueError: at index 0: not a LatLng or LatLngLiteral: not an Object?
- html - 内联块内的绝对 div,相对父 div 采用兄弟的宽度