Swift Generics,Constraints和KeyPaths

时间:2018-02-16 02:27:52

标签: swift generics nssortdescriptor keypaths

我已经意识到Swift中泛型的局限性以及它们存在的原因所以这不是关于编译器错误的问题。相反,我偶尔会遇到这样的情况,好像它们应该可以通过某些资源组合(即泛型,相关类型/协议等)来实现,但似乎无法找到解决方案。

在这个例子中,我试图为NSSortDescriptor提供一个Swift替换(只是为了好玩)。当你只有一个描述符时它很完美,但是,正如通常使用NS版本一样,创建一个SortDescriptors数组以对多个键进行排序会很好。

这里的另一个试验是使用Swift KeyPaths。因为那些需要Value类型并且比较需要Comparable值,所以我遇到了麻烦,找出在何处/如何定义类型以满足所有内容。

这可能吗?这是我提出的最接近的解决方案之一,但是,正如您在底部看到的那样,在构建阵列时它无法实现。

同样,我理解为什么它不能按原样运行,但我很好奇是否有办法实现所需的功能。

struct Person {
    let name : String
    let age : Int

}
struct SortDescriptor<T, V:Comparable> {
    let keyPath: KeyPath<T,V>
    let ascending : Bool
    init(_ keyPath: KeyPath<T,V>, ascending:Bool = true) {
        self.keyPath = keyPath
        self.ascending = ascending
    }
    func compare(obj:T, other:T) -> Bool {
        let v1 = obj[keyPath: keyPath]
        let v2 = other[keyPath: keyPath]
        return ascending ? v1 < v2 : v2 < v1
    }
}

let jim = Person(name: "Jim", age: 30)
let bob = Person(name: "Bob", age: 35)
let older = SortDescriptor(\Person.age).compare(obj: jim, other: bob) // true

// Heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional
var descriptors = [SortDescriptor(\Person.age), SortDescriptor(\Person.name)]

3 个答案:

答案 0 :(得分:7)

此处的问题是SortDescriptorTV上都是通用的,但您只希望它在T上是通用的。也就是说,您需要一个SortDescriptor<Person>,因为您关心的是Person。您不需要SortDescriptor<Person, String>,因为一旦创建它,​​您就不在乎它在String的某些Person属性上进行比较。

“隐藏”V的最简单方法可能是使用闭包来包装关键路径:

struct SortDescriptor<T> {
    var ascending: Bool

    var primitiveCompare: (T, T) -> Bool

    init<V: Comparable>(keyPath: KeyPath<T, V>, ascending: Bool = true) {
        primitiveCompare = { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
        self.ascending = ascending
    }

    func compare(_ a: T, _ b: T) -> Bool {
        return ascending ? primitiveCompare(a, b) : primitiveCompare(b, a)
    }
}

var descriptors = [SortDescriptor(keyPath: \Person.name), SortDescriptor(keyPath: \.age)]
// Inferred type: [SortDescriptor<Person>]

之后,您可能想要一种方便的方法来使用SortDescriptor序列来比较对象。为此,我们需要一个协议:

protocol Comparer {
    associatedtype Compared
    func compare(_ a: Compared, _ b: Compared) -> Bool
}

extension SortDescriptor: Comparer { }

然后我们可以使用Sequence方法扩展compare

extension Sequence where Element: Comparer {

    func compare(_ a: Element.Compared, _ b: Element.Compared) -> Bool {
        for comparer in self {
            if comparer.compare(a, b) { return true }
            if comparer.compare(b, a) { return false }
        }
        return false
    }

}

descriptors.compare(jim, bob)
// false

如果您使用具有条件一致性的较新版本的Swift,则应该能够通过将扩展的第一行更改为此来有条件地将Sequence符合Comparer

extension Sequence: Comparer where Element: Comparer {

答案 1 :(得分:3)

扩展@Rob Mayoff的回答,这是一个完整的排序解决方案

enum SortDescriptorComparison {
    case equal
    case greaterThan
    case lessThan
}

struct SortDescriptor<T> {
    private let compare: (T, T) -> SortDescriptorComparison
    let ascending : Bool

    init<V: Comparable>(_ keyPath: KeyPath<T,V>, ascending:Bool = true) {
        self.compare = {
            let v1 = $0[keyPath: keyPath]
            let v2 = $1[keyPath: keyPath]
            if v1 == v2 {
                return .equal
            } else if v1 > v2 {
                return .greaterThan
            } else {
                return .lessThan
            }
        }
        self.ascending = ascending
    }

    func compare(v1:T, v2:T) -> SortDescriptorComparison {
        return compare(v1, v2)
    }
}

extension Array {

    mutating func sort(sortDescriptor: SortDescriptor<Element>) {
        self.sort(sortDescriptors: [sortDescriptor])
    }

    mutating func sort(sortDescriptors: [SortDescriptor<Element>]) {
        self.sort() {
            for sortDescriptor in sortDescriptors {
                switch sortDescriptor.compare(v1: $0, v2: $1) {
                case .equal:
                    break
                case .greaterThan:
                    return !sortDescriptor.ascending
                case .lessThan:
                    return sortDescriptor.ascending
                }
            }
            return false
        }
    }
}

extension Sequence {

    func sorted(sortDescriptor: SortDescriptor<Element>) -> [Element] {
        return self.sorted(sortDescriptors: [sortDescriptor])
    }

    func sorted(sortDescriptors: [SortDescriptor<Element>]) -> [Element] {
        return self.sorted() {
            for sortDescriptor in sortDescriptors {
                switch sortDescriptor.compare(v1: $0, v2: $1) {
                case .equal:
                    break
                case .greaterThan:
                    return !sortDescriptor.ascending
                case .lessThan:
                    return sortDescriptor.ascending
                }
            }
            return false
        }
    }
}

struct Person {
    let name : String
    let age : Int
}

let jim = Person(name: "Jim", age: 25)
let bob = Person(name: "Bob", age: 30)
let tim = Person(name: "Tim", age: 25)
let abe = Person(name: "Abe", age: 20)

let people = [tim, jim, bob, abe]
let sorted = people.sorted(sortDescriptors: [SortDescriptor(\Person.age), SortDescriptor(\Person.name)])

print(sorted) //Abe, Jim, Time, Bob

答案 2 :(得分:0)

这是一个几乎纯粹功能性的解决方案:

// let's add some semantics
typealias SortDescriptor<T> = (T, T) -> Bool

// type constructor for SortDescriptor
func sortDescriptor<T, U: Comparable>(keyPath: KeyPath<T, U>, ascending: Bool) -> SortDescriptor<T> {
    return { ascending == ($0[keyPath: keyPath] < $1[keyPath: keyPath]) }
}

// returns a function that can sort any two element of type T, based on
// the provided list of descriptors
func compare<T>(with descriptors: [SortDescriptor<T>]) -> (T, T) -> Bool {
    func innerCompare(descriptors: ArraySlice<SortDescriptor<T>>, a: T, b: T) -> Bool {
        guard let descriptor = descriptors.first else { return false }
        if descriptor(a, b) { return true }
        else if descriptor(b, a) { return false }
        else { return innerCompare(descriptors: descriptors.dropFirst(1), a: a, b: b) }
    }
    return { a, b in innerCompare(descriptors: descriptors[0...], a: a, b: b) }
}

// back to imperative, extend Sequence to allow sorting with descriptors
extension Sequence {
    func sorted(by descriptors: [SortDescriptor<Element>]) -> [Element] {
        return sorted(by: compare(with: descriptors))
    }
}

它基于小型,可重复使用的功能,如compare(),可以在其他范围内轻松重复使用。

用法示例:

struct Person {
    let name : String
    let age : Int
}

let jim = Person(name: "Jim", age: 30)
let bob = Person(name: "Bob", age: 35)
let alice = Person(name: "Alice", age: 35)
let aly = Person(name: "Aly", age: 32)

let descriptors = [sortDescriptor(keyPath: \Person.age, ascending: false),
                   sortDescriptor(keyPath: \Person.name, ascending: true)]
let persons = [jim, bob, alice, aly]
print(persons.sorted(by: descriptors))