That's a fine approach to cloning.
However, I'm not offering this proposal as an alternative to something like that, nor is this meant as an alternative to the great Explicit Memberwise Initializers proposal of Matthew Johnson.
Rather, the point of Compositional Initialization is to allow an object to be composed before it is initialized from a set of type-safe keypath-value pairs.
In the current form of this proposal, these pairings take the form of Property<Root,Value> objects.
You can look at this proposal as building upon the foundation laid by KeyPath.
See, in Swift 4 we got KeyPath, but we did not get any easy way to use a KeyPath to initialize an object or to make a clone mutated at some property—nor did we get a type like Property allowing the developer to pair a KeyPath with a Value.
Given that it seems natural to pair KeyPaths and Values, once you have a set of these pairs, it seems natural then to make an object from them.
So this proposal offers a simple way to do that.
Here is a working example from the Swift 5 Playground linked in the top post:
struct Customer: PropertyInitializable {
var name: String = ""
var zipcode: Int = 0
var addressLine1: String = ""
var addressLine2: String = ""
var addressLine3: String = ""
/// Necessary because of the fact WritableKeyPath cannot currently be used
/// to actually initialize a variable. It must have a value first.
internal static var _blank: Customer {
get { return Customer() }
}
}
let dataFromTestWebResponse = [PartialProperty<Customer>]()
+ (\.name, "Steve Jobs")
+ (\.zipcode, 97202)
+ (\.addressLine1, "Reed College")
+ (\.addressLine2, "3203 SE Woodstock Blvd")
+ (\.addressLine3, "Box #121")
if let customer: Customer = Customer(dataFromTestWebResponse) {
print(customer)
} else {
print("Data is missing.")
}
As this example shows, we can create a type-safe array of properties for an object using nothing but tuples. (This relies on a custom infix function + defined on an extension to Array; please see the linked Playground page for the full source.) We can then try to initialize the object from that array.
The failable init method is defined in PropertyInitializable protocol as:
init?(_ properties: [PartialProperty<Self>])
The elements of the dataFromTestWebResponse array are of the type PartialProperty<Customer>.
At first glance, that array might not seem to be fully type-safe.
However, the underlying objects are type-safe Property<Customer,Value> objects that have been partially type-erased so they can exist within the same array. This is necessary because they differ as to the type of Value—zipcode is an Int but the rest are String. (Note: this naming convention follows exactly from Swift's existing PartialKeyPath and KeyPath.)
In this way, you can "compose" the properties of an object in a completely type-safe way prior to initialization, and then initialize the object from those properties with minimal boilerplate.
For example we might do:
let reedieData = [PartialProperty<Customer>]()
+ (\.zipcode, 97202)
+ (\.addressLine1, "Reed College")
+ (\.addressLine2, "3203 SE Woodstock Blvd")
let studentBoxAssignments: [(String, Int)] = [("Steve Jobs", 121), ("The Woz", 314)]
var customers = [Customer]()
for (name, box) in studentBoxAssignments {
let data = reedieData + (\.name, name) + (\.addressLine3, "Box #\(box)")
if let customer: Customer = Customer(data) {
customers += [customer]
} else {
print("Data is missing.")
}
}
print(customers)
By saying this is completely type-safe, I mean, if we put "97202" as a string then we'll get a compiler error, since the proper type is inferred from the KeyPath.
Now, let me address the reasoning behind the => syntax, from the example in the original post:
let properties: [PartialProperty<Foo>] =
[
\.bar => “two”,
\.baz => 2.0,
\.quux => nil
]
Frankly, this is just a simple alternative to using the tuples syntax from the examples above. By defining => as a custom infix operator, we can do away with the parentheses around each key-value pair.
If => seems un-Swifty, I'd love to hear a better suggestion. Swift's custom operator syntax does not permit the use of : when defining a custom infix operator here; that would make this a Dictionary.
Speaking of Dictionaries, I originally tried using Dictionary instead of Array, since it seemed like a more natural place to store keypath-value pairs. However, I found using a Dictionary for this purpose to be frought with problems. Let me know if you want the details on why that did not seem feasible.