Skip to content
Discussion options

You must be logged in to vote

You can use separate type parameters for the inputs and return their union:

def combine[T, U](
    first: Cache[T],
    second: Cache[U],
) -> Cache[T | U]:
    return Cache(first.items + second.items)

For Cache[int] and Cache[str], the result is Cache[int | str]. This keeps both element types without requiring them to be identical.

Since list is invariant, the result won't automatically be assignable to Cache[object], but Cache[T | U] is the appropriate type if the combined cache can contain values of either type. Hope that helps!!

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Agostin12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants