Swift API for callable functions #8927
Replies: 3 comments 3 replies
-
To expand on the last comment above: If we use
Point of use:
|
Beta Was this translation helpful? Give feedback.
-
@peterfriese @paulb777 @IanWyszynski Just tagging you in case you would find this discussion interesting. 😊 |
Beta Was this translation helpful? Give feedback.
-
Hi all,
Thus removing the requirement for specifying the return type at the call site and eliminating calls with unintended data, but supplying the types directly to Thank you for the suggestion, Sebastian. I'll go ahead and close the discussion, but if anyone has further comments you are of course welcome to reopen it. |
Beta Was this translation helpful? Give feedback.
-
Hi all,
Adding encoding and decoding support - and async/await to the callable api is pretty straight forward and there's a PR in place at: #8854
But the API could perhaps be made a bit 'stronger': A callable function is generally parametrized by three things: A name for the callable, the type of the encodable request and the type of the decodable response.
This means that you could create a protocol whose conforming types would correspond to the definition of a callable function:
(or CallableDefinition or some other name)
Defining your callables through this API gives more type safety (less risk of accidentally creating typos at the point of use, or using other data than the expected for either request or response).
Example of the usage of the API defined in the PR versus one with tighter typing - and here I'm using the async/await versions of the API because they are a bit simpler to type out, but the same of course applies for non-async/await API:
Current:
versus the suggested:
In the latter, the compiler enforces that request must be a CalculatePiRequest and it returns a CalculatePiResponse.
My question for discussion is:
Should this more opinionated API be made available?
Should the less opinionated API be discouraged (by not adding codable extension to it)
Should we leave the less opinionated API as the only option since it resembles the underlying API more closely.
In my own code I love to have strong typing and less opportunity for errors by defining these kinds of 'schema'-types. I do the same for RTDB and Firestore. So personally I'd go for adding the 'type safe' API and not adding codable extensions to the existing API.
What do you think?
One extra addition could be to use the 'callAsFunction' functionality so that you don't require the extra .call on a callable, but you can treat it as a function...
Beta Was this translation helpful? Give feedback.
All reactions