Created
January 16, 2022 11:00
-
-
Save daneden/a17f57efd275de544791df6b31f845bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Fields { | |
var userFields: Set<User.Fields>? | |
var tweetFields: Set<Tweet.Fields>? | |
} | |
protocol Fetcher { | |
// Is there a way to do something like this, where I can specify that the `Fields` | |
// struct must contain only these member types? | |
func getTweet(fields: Fields<Tweet.Fields, User.Fields>? = nil) async -> Tweet | |
func getUser(fields: Fields<User.Fields>? = nil) async -> User | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@fourplusone thanks for the reply and suggestion! Separate fields parameters is what I originally had in the function design but some functions could accept up to 5 distinct field types so function calls could get pretty unwieldy.
This helped me think about another way to express it though. Since the function calls with multiple field types usually follow a pattern (a single primary field type such as
Tweet.Fields
, and then one or more secondary field types for expansions like Users, Polls, Media, etc), I can probably make the secondary field types associated values on the expansions enum. This has two advantages: removes the need for the generic Fields struct to act as a container, and ensures only field types that are allowed with the expected expansions.Example function call after this change: