Drugan
Drugan

Reputation: 144

Protocol composition confusing

I have protocols SocketIORepresentable and Typeable. Typeable is protocol with associatedType. Why I have error when i try to create protocol composition in my func?

swift 5.0

import Foundation

protocol Typeable {
    associatedtype Item: Decodable
    var responseType: Item.Type { get }
}


protocol SocketIORepresentable {
    func representable() throws -> [Any]
}

extension SocketIORepresentable where Self: Encodable {
    func representable() throws -> [Any] {
        let tempSendData = try JSONEncoder().encode(self)
        return [try JSONSerialization.jsonObject(with: tempSendData, options: .allowFragments)]
    }
}

public struct APIRequest: Encodable {
    let type: String
    let payload: Data
    let ts = Date()
}

extension APIRequest: SocketIORepresentable {}

public struct Request<T: Decodable>: Typeable {
    let apiRequest: APIRequest
    var responseType: T.Type
    public init(apiRequest: APIRequest) {
        self.apiRequest = apiRequest
        self.responseType = T.self
    }
}

private func sendRequest<T: Typeable>(_ request: T & SocketIORepresentable, completion: @escaping (Result<T.Item, Error>) -> Void) {

}

Upvotes: 0

Views: 67

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

T is not a protocol. T is a concrete type that conforms to Typeable. You cannot then say that request must be "of the concrete type T and also conforms to SocketIORepresentable." What you meant is that T is a concrete type that conforms to both:

private func sendRequest<T: Typeable & SocketIORepresentable>(_ request: T, completion: @escaping (Result<T.Item, Error>) -> Void) { ... }

Typically you'd write this with a where clause instead to avoid such a large angle-bracket:

private func sendRequest<T>(_ request: T, completion: @escaping (Result<T.Item, Error>) -> Void)
    where T : Typeable & SocketIORepresentable { ... }

Upvotes: 3

Related Questions