Maybe just make do
an expression?
let widget = do {
var w = Widget()
w.identifier = newId
w.color = .red
w.width = 100
w.angle = 2
w
}
Potentially it could be enriched by an optional init
keyword:
let widget = do init Widget {
self.init()
identifier = newId
color = .red
width = 100
angle = 2
}
which is in fact an inline version of init
extension:
extension Widget {
init(identifier: Int, color: Color, width: CGFloat, angle: Double) {
self.init()
self.identifier = identifier
self.color = color
self.width = width
self.angle = angle
}
}
But why to stop on inits? We could support inline extensions in general:
do init Type { ... } // equivalent of extension init
do with existingValue { ... } // equivalent of extension func, the return type is inferred by the body
do mutating existingValue { ... } // equivalent of extension mutating func, the return type is inferred by the body
do consuming existingValue { ... } // equivalent of extension consuming func, the return type is inferred by the body
do modifying existingValue { ... } // equivalent of extension func that is forced to return self