It seems like at least one big reason people want this is to avoid reformatting during print debugging, etc. I use the following custom ~>
operator based on ideas from the "With functions" pitch.:
@inlinable @discardableResult public func ~> <T>(_ value: T, modifications: (inout T) throws -> Void) rethrows -> T {
var value = value
try modifications(&value)
return value
}
// Typical usage
let rect: CGRect = self.parentRect ~> { $0.size.width = 320.0 }
Besides allowing values to be modified in place, it can also be used inside of expressions to ensure they're still a single statement and avoid the parenthesis on an anonymous closure. You can simply add it to the end of the line without adding an extra return.
func doSomething(value: Int) -> Bool {
switch value {
case .min..<0: true
case 0..<1000: false ~> { _ in
print("Why did this happen?? \(value)") // Set breakpoint here
}
default: true
}
}
var goodGetter: Bool {
self.value ~> { _ in print("Using someGetter \(self.value") }
}
var grossGetter: Bool {
print("Using someGetter \(self.value)")
return self.value
}