[swift-evolution] Request for Discussion: Setup closures
David Waite
david at alkaline-solutions.com
Fri Dec 4 16:55:52 CST 2015
This seems related to my proposal to be able to bind self to a closure parameter.
Instead of:
with let task = NSTask() {…}
you could just make ‘with' a standard function:
func with<T>(argument:T, apply:(T) throws->()) rethrows -> T {
try apply(argument)
return argument
}
let task = with(NSTask()) {
$0.launchPath = …
}
Then, with my proposal:
let task = with(NSTask()) {
self in
launchPath = …
arguments = …
standardOutput = ...
}
-DW
> On Dec 4, 2015, at 2:51 PM, Michel Fortin <michel.fortin at michelf.ca> wrote:
>
> Le 4 déc. 2015 à 16:35, Sean Heber <sean at fifthace.com> a écrit :
>
>> I would also be in favor of something more generally applicable rather than making initialization more special. I think I prefer the block-style over Dart’s approach. Smalltalk uses semicolons for this, if I recall. I’m not sure I like using a special operator. I’m obviously a keyword guy.. :P
>>
>> with let task = NSTask() {
>> launchPath = "/usr/bin/mdfind"
>> arguments = ["kMDItemDisplayName == *.playground"]
>> standardOutput = pipe
>> }
>>
>> Also valid:
>>
>> with someVariable {
>> func1()
>> func2(“etc")
>> }
>>
>> Which would call func1() and func2() on the someVariable instance (which I think would be quite expected).
>
> The problem with this approach is that it can become ambiguous with the outer scope pretty easily, if not to the compiler at least for the reader:
>
> let launchPath = "a.out"
> let description = "blah blah"
> with let task = NSTask() {
> launchPath = launchPath // eh, what?
> arguments = [launchPath, description] // is that task.description?
> }
>
> I think it's important that things referring to the "task" in the above example be syntactically distinguishable.
>
> It can also lead to code breakage:
>
> let path = "a.out"
> with let task = NSTask() {
> launchPath = path
> }
>
> That would work fine one day, but what if in the next OS release NSTask gets a new "path" property? That code breaks silently once you recompile with the newer SDK.
>
>
> --
> Michel Fortin
> michel.fortin at michelf.ca
> https://michelf.ca
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list