Can we bump this topic for Swift 5?
Similar to how KeyPaths have a language support by extending Any
we could provide a with
function to any type.
extension Any {
@discardableResult
public func with(update: (inout Self) throws -> Void) rethrows -> Self {
var this = self
try update(&this)
return this
}
}
If we had a distinction between AnyObject
and (not existing) AnyValue
we could extend those instead in favor of a inout
free with
function for classes and also excluding function reference types.
One advantage the extension of Any
adds over the pitched global function is that you can use it in combination with optional chaining.
var text: String? = ...
label.text = text?.with { /* mutate non-optional string */ }