Tuple unpacking #238
Replies: 1 comment
-
I'm not aware of such a thing. It would probably require variadic generics to work if you want to avoid a huge number of overloads. let x = ((1,"A"), "Z")
let flattened = unsafeBitCast(x, to: (Int, String, String).self)
flattened == (1, "A", "Z") If your target type can be reached by various nesting configurations, you can also conveniently declare a struct to hold the values: struct MyStruct {
var count: Int
var first: String
var last: String
}
let myStruct1 = unsafeBitCast(((1,"A"), "Z"), to: MyStruct.self)
let myStruct2 = unsafeBitCast((1,("A", "Z")), to: MyStruct.self)
myStruct1 == myStruct2 If you go this route, you should probably Structs are not mandatory, and you can also work with flat tuples all the way instead, but it will be likely more cumbersome. |
Beta Was this translation helpful? Give feedback.
-
I'm working with a few parsers that return tuples. Therefore once using them in a nested fashion I get nested tuples such as:
((Int, Int), Int, Int)
Any advice on how to easily unpack them? Maybe worth adding some generic conversions for to the library? (If feasible because I can picture the amount of possibilities exploding)Beta Was this translation helpful? Give feedback.
All reactions