I'm currently writing some code that expects me to be able to turn Optional<(T1, T2, T3)> into (Optional<T1>, Optional<T2>, Optional<T3>), for any variable-length tuple in order to do destructuring using if-let and if-case-let bindings. I could theoretically come up with variations by hand, but since this is part of a Swift code generator I must be able to handle any crazy construction the user throws at it.
I tried to tackle this with parameter packs, but the best I can come up with still needs an indirection to populate the (nil, nil, ...) value:
func shuffle<each T>(_ tuple: (repeat each T)?) -> (repeat Optional<each T>) {
if let tuple = tuple {
return (repeat each tuple)
}
func _dummy<U>(_ any: U.Type) -> U? { nil }
return (repeat _dummy((each T).self))
}
I'm wondering if there's a cleaner way of doing it?