See my pitch for Compositional Initialization:
This addresses the exact thing you're asking about.
As well, it would offer a novel, albeit non-idiomatic, approach to your first question:
extension Location: PropertyInitializable {
static var _blank: Location {
return Self(x: 0, y: 0, a: 0)
}
}
func process(_ results: MyResult<Int> ...) -> MyResult<Location> {
var properties = [PartialProperty<Location>]()
for (i, r) in results.enumerated() {
switch (i, r) {
case let (_, .failure(err)):
return .failure(err)
case let (i, .success(n)):
var path: WritableKeyPath<Location, Int>
switch i {
case 0: path = \.x
case 1: path = \.y
case 2: path = \.a
default: fatalError()
}
properties += try! [path <- n]
}
}
guard let loc = Location(properties) else {
return .failure(.someError)
}
return .success(loc)
}
let result = process(r1, r2, r3)