在 Swift 中,我有一个具有以下基本前提的自定义结构:
一个包装结构体,可以包含符合以下条件的任何类型BinaryInteger
如Int、UInt8、Int16等
protocol SomeTypeProtocol {
associatedtype NumberType
var value: NumberType { get set }
}
struct SomeType<T: BinaryInteger>: SomeTypeProtocol {
typealias NumberType = T
var value: NumberType
}
还有 Collection 的扩展:
extension Collection where Element: SomeTypeProtocol {
var values: [Element.NumberType] {
return self.map { $0.value }
}
}
例如,这很好用:
let arr = [SomeType(value: 123), SomeType(value: 456)]
// this produces [123, 456] of type [Int] since literals are Int by default
arr.values
我想做的是完全相同的事情,但是对于SomeType<T>?
let arr: [SomeType<Int>?] = [SomeType(value: 123), SomeType(value: 456)]
// this doesn't work, obviously
arr.values
// but what I want is this:
arr.values // to produce [Optional(123), Optional(456)]
我已经尝试了无数次尝试来解决这个问题,并进行了大量的研究,但我希望任何明智的 Swift 老手都能对此有所启发。
这就是我想象的样子,但这行不通:
extension Collection where Element == Optional<SomeType<T>> {
var values: [T?] {
return self.map { $0?.value }
}
}
这是一种在不使用泛型的情况下实现目标的笨拙方法,但它有效:
extension Collection where Element == Optional<SomeType<Int>> {
var values: [Int?] {
return self.map { $0?.value }
}
}
let arr: [SomeType<Int>?] = [SomeType(value: 123), SomeType(value: 456)]
arr.values // [Optional(123), Optional(456)]
但它需要为每个符合 BinaryInteger 的已知类型手动编写扩展,并且在不手动更新代码的情况下不会自动包含采用 BinaryInteger 的未来可能的类型。
// "..." would contain the var values code from above, copy-and-pasted
extension Collection where Element == Optional<SomeType<Int>> { ... }
extension Collection where Element == Optional<SomeType<Int8>> { ... }
extension Collection where Element == Optional<SomeType<UInt8>> { ... }
extension Collection where Element == Optional<SomeType<Int16>> { ... }
extension Collection where Element == Optional<SomeType<UInt16>> { ... }
extension Collection where Element == Optional<SomeType<Int32>> { ... }
extension Collection where Element == Optional<SomeType<UInt32>> { ... }
extension Collection where Element == Optional<SomeType<Int64>> { ... }
extension Collection where Element == Optional<SomeType<UInt64>> { ... }
2018 年 6 月 23 日编辑:
解决方案 #1 - 完全通用但必须是 Func,而不是计算属性
扩展于奥莱的回复 https://stackoverflow.com/a/51001112/2805570:
优点:如果values()
成为一个func
而不是计算属性,这是一个优雅的解决方案。
缺点:没有已知的方法可以将此方法实现为计算属性,并且 Swift 的快速帮助弹出窗口在检查时显示 [T] 和 [T?]values()
在你的代码中。即:它只是说func values<T>() -> [T] where T : BinaryInteger
这不是很有信息或 Swifty。然而,它当然仍然是强类型的。
extension Collection {
func values<T>() -> [T] where Element == SomeType<T> {
return map { $0.value }
}
func values<T>() -> [T?] where Element == SomeType<T>? {
return map { $0?.value }
}
}
解决方案 #2 - 可选协议解决方法
扩展于马丁的回复 https://stackoverflow.com/a/50999846/2805570:
优点:允许使用计算属性(最终用户访问起来更干净,因为它不需要 func 括号)并在 Xcode 的快速帮助弹出窗口中显示推断的类型。
缺点:从内部代码的角度来看并不那么优雅,因为它需要解决方法。但不一定是缺点。
// Define OptionalType
protocol OptionalType {
associatedtype Wrapped
var asOptional: Wrapped? { get }
}
extension Optional: OptionalType {
var asOptional: Wrapped? {
return self
}
}
// Extend Collection
extension Collection where Element: SomeTypeProtocol {
var values: [Element.NumberType] {
return self.map { $0.value }
}
}
extension Collection where Element: OptionalType, Element.Wrapped: SomeTypeProtocol {
var values: [Element.Wrapped.NumberType?] {
return self.map { $0.asOptional?.value }
}
}