4 ms·
You can nearly implement your version of `with` today: func with<T>(value: T, f: T -> Void) -> T { f(value) return value } Which you c
by mikeash 11y ago
You can nearly implement your version of `with` today:
func with<T>(value: T, f: T -> Void) -> T {
f(value)
return value
}
Which you could then use like:
let task = with(NSTask()) {
$0.launchPath = "/usr/bin/mdfind"
$0.arguments = ["kMDItemDisplayName == *.playground"]
$0.standardOutput = pipe
$0.launch()
$0.waitUntilExit()
}
The `let task =` part would be optional, and you could leave it off if you don't need to use the variable later.
- kbenson 11y agoOkay, then I don't really see a reason for Erica's syntax,which is special and either ambiguous or limited. How do you know whether you are calling a method on the given object, or a plain function? How to you know whether you are using an attribute of the object or a variable? Just rely on scope? That seems far more problematic and error prone for the small convenience of not typing three more characters with object access. Your solution is actually simple enough that I'm not sure there needs to be a change to the language, unless there's some special behavior they can and should impart that we aren't thinking if. That said, I don't write swift, so feel free to take my opinion for whatever you think it's worth. :)
- chris_7 11y agoI'm not sure if the function call could be optimized out across module boundaries, that's really the only justification for this to be dedicated syntax I can see. It's somewhat obsoleted in Swift 3 regardless as SPM uses static linking, where it should be able to be optimized.