Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <…>)

前端 未结 1 1980
醉酒成梦
醉酒成梦 2021-01-16 10:11

I have a class that takes a generic class Collection: (Model is a class) and a protocol (Resource) that some of the s

1条回答
  •  梦谈多话
    2021-01-16 11:00

    The problem is Collection is a generic class so every where you declare it, you must attached the specialized type, like Collection. However extension to a protocol can't be specified with a generic type so you end up not being able to supply T to Collection.

    In your case though, T is constrained to be of type Model so why not use that in the default protocol implementation:

    extension Resource where Self: Collection {
        func fetch() {
            // default implementation
        }
    }
    

    0 讨论(0)
提交回复
热议问题