Weird behaviour with protocol conforming to itself

When I run the following extension code on macOS Monterey 12.6 with Xcode 14.0.1 in a playground, it compiles and runs fine.

However when I run it on my laptop on macOS Big Sur 11.7 with Xcode 13.2.1 it gives me a compilation error saying "Protocol Encodable as a type cannot conform to the protocol itself".

My project iOS deployment target is 15.2

Has something changed in recent version so that now this works?

import SwiftUI

extension URLRequest {
    init?(baseUrl: String, controller: String, action: String = String(), httpBody: Encodable? = nil) {
        let url = "\(baseUrl)/\(controller)/\(action)"
        guard let url = URL(string: url) else {
            return nil
        }

        self.init(url: url)
        self.setValue(URLRequest.Constants.applicationJson, forHTTPHeaderField: URLRequest.Constants.headerContentType)
        
        if let httpBody = httpBody {
            // here is where it messes up on my laptop
            guard let httpBody = try? JSONEncoder().encode(httpBody) else {
                return nil
            }
            
            self.httpBody = httpBody
        }
    }
}

struct AuthenticationInfo: Encodable {
    var username: String
    var password: String
}

let urlRequest = URLRequest(
    baseUrl: "http://1.2.3.4:12345",
    controller: "mycontroller",
    action: "myaction",
    httpBody: AuthenticationInfo(username: "myusername", password: "mypassword"))

Yes, your code is making use of implicitly opened existentials.

I think the fact that this “just worked” is a good sign that implicitly opened existentials were well-designed.

1 Like

Thank you.

I guess I shouldn't have taken dependency injection for granted.