I posted this issue on the Feedback Assistant at Apple... but this really seems to be on Swift's part.
// MARK: - Models
public struct Donut {
public var flavor: String
public var isGlutenFree: Bool
public var isVegan: Bool
}
// MARK: - Endpoint Protocol
public protocol Endpoint {
associatedtype Result
}
extension Endpoint {
public typealias donuts = MyEndpoints.GetDonuts
}
// MARK: - Endpoints
public enum MyEndpoints {
public struct GetDonuts: Endpoint {
public typealias Result = [Donut]
var glutenFree: Bool = false
var vegan: Bool = false
public init(glutenFree: Bool = false, vegan: Bool = false) {
self.glutenFree = glutenFree
self.vegan = vegan
}
}
}
// MARK: - Client Protocol
public protocol ClientProtocol {
func get<T: Endpoint>(_ endpoint: T) async throws -> T.Result
}
// MARK: - Example
func someExample(_ client: ClientProtocol) async throws {
// Using non-typealias syntax works fine
let result0 = try await client.get(MyEndpoints.GetDonuts(glutenFree: true))
// result0 is of type Array<Donut> as expected
// Using typelias `donuts` in protocol Endpoint extension
let result1 = try await client.get(.donuts())
// doesn't work compile
}
Why would I expose my endpoints like this ?
Because it's a nice syntax,
- autocompletion of optional values is lovely with Xcode 14 beta
- autocompletion of available typealiases exists as well ...
Nothing prevents me to write the typealias and the type alias is correct.