Open PangMo5 opened 1 year ago
Thanks for suggestion @PangMo5 - I'd definitely love if we could support this!
At one point I actually had the @Default
functionality as you suggested but unfortunately we lost the ability to do that in the Xcode 15 Beta update that post you linked is about.
Also unfortunately, Swift doesn't support either default arguments (your first example) or property wrappers (potential other implementation) in protocol definitions yet. I don't believe there is another way to do it, otherwise I'd be very open to it. If you have any ideas of how this could be accomplished let me know!
For now, however, you can achieve the desired effect using (albeit verbose) extensions.
@API
protocol Users {
@GET("/users")
func getList(size: Int, cursor: String?) async throws -> [User]
@POST("/user")
func createUser(email: String, password: String, nickname: String?) async throws
}
// note that the @GET / other macros aren't needed in the extension.
extension Users {
func getList(size: Int = 20, cursor: String?) async throws -> [User] {
try await getList(size: size, cursor: cursor)
}
func createUser(email: String, password: String, nickname: String? = nil) async throws {
try await createUser(email: email, password: password, nickname: nickname)
}
}
Again - this is pretty verbose and I think your first example is the ideal solution, but default arguments aren't currently allowed in protocols.
@joshuawright11 , Is it possible to generate such Users extension by something like that?
@API
protocol Users {
@GET("/users", defaults: ["size": 20])
func getList(size: Int, cursor: String?) async throws -> [User]
@POST("/user", defaults: ["nickname": nil])
func createUser(email: String, password: String, nickname: String?) async throws
}
Looks not very type safe but probably we can match keys from defaults
with endpoint's ages and verify values with appropriate types as well (and throw error if needed).
Hi, first of all, thanks for writing such a nice macros.
There was one thing that I found disappointing. The Swift syntax does not allow you to set default values for parameters in protocol functions. If it's a normal protocol function, you can set the default value of the function parameter through an extension, but since it's a macro, it doesn't seem possible.
I don't know much about macros, but here's what I can think of.
1.
If it is possible to have an implementation that violates Swift syntax like the one above with just a macros, then the above seems to be the most convenient and reasonable.
2.
Looking at the comments in the code and the post at, it seems that this is probably not possible at the moment, as there is no macros specification. If a spec is added in the future, this would be fine.
In addition to this, if there is a way to do this in the current implementation without a new one, I'd to hear about it!