How can I extend dictionaries whose values are dictionaries themselves?

不羁岁月 提交于 2019-12-12 01:22:52

问题


Say I want to extend nested dictionaries with some functionality. Using pseudo-Swift, this is my goal:

extension Dictionary where Value: Dictionary {
    typealias K1 = Key
    typealias K2 = Value.Key
    typealias V  = Value.Value

    subscript(k1: K1, k2: K2) -> V? {
        return self[k1]?[k2]
    }
}

I can not get this to work, though. Type bounds can not be non-protocol types; no protocol that Dictionary implements provides the methods I and types I need to refer to; getting access to the types of generics is cumbersome; and so on. Nothing I tried works out.

What is a solution for this?


回答1:


One trick (dare I say pattern?) we can use here is to define our own protocol (which we never intend to use anywhere else) which declares all we need and we know Dictionary conforms to, anyway.

protocol DictionaryProtocol {
    associatedtype Key: Hashable
    associatedtype Value

    subscript(key: Key) -> Value? { get set }
}

extension Dictionary: DictionaryProtocol {}

extension Dictionary where Value: DictionaryProtocol {
    typealias K1 = Key
    typealias K2 = Value.Key
    typealias V  = Value.Value

    subscript(k1: K1, k2: K2) -> V? {
        return self[k1]?[k2]
    }
}

This solution carries over to arrays of arrays, and probably many similar cases.



来源:https://stackoverflow.com/questions/41569749/how-can-i-extend-dictionaries-whose-values-are-dictionaries-themselves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!