This would be a new initializer for Array
, allowing it to be created from a Subject
, targeted at providing greater ease-of-use to tuples.
The problem with Tuples
Homogenous tuples are already the standard way of representing fixed-sized arrays from C, and while there is this proposal which could potentially introduce fixed-sized arrays, there are still other valid reasons to treat a tuple like an array or other collection. For instance, printing the contents of a tuple element-by-element currently requires either using Mirror
or printing each element with print(myTuple.0); print(myTuple.1)
... et cetera.
One solution to this is simply making Tuple into its own collection by making it conform to the various protocols, but this is riddled with issues. Tuples are of course meant to be temporary values, and I worry that empowering them as collections encourages programmers to lean on them in places where other collections, or even structs, might be more appropriate. Heterogenous tuples would be problematic for iteration and mapping:
let myTuple = (1, 2, "Hello")
for element in myTuple {
// element would be of type Any, which isn't really clear
}
Lastly, even if tuples conform to Collection
, there could still be more array-like use cases that are left unsatisfied. For instance, there might be a need for a tuple concatenation method similar to append(contentsOf: )
.
Array initialization from Subject
One solution for this that I'm pondering is to allow Array
s to be initialized from Subject
, something that I already regularly add to my projects as an extension:
extension Array {
init?<Subject>(mirroredFrom subject: Subject) {
guard let array = Mirror(reflecting: subject).children.map(\.value) as? Self
else {
return nil
}
self = array
}
}
Some of the benefits of this:
- Encourages converting tuples to arrays if you want to use them for long-term purposes
- Gives access to all of the benefits of
Collection
and to all ofArray
's methods. - Long arrays are preferable to pass into functions than long tuples, which are destructured into individual arguments
- Heterogenous tuples can't be converted
- Allows for array initialization from other reflectables, which could be a benefit or a drawback. (Do we really want to be able to convert structs to arrays?)