[swift-evolution] Request for Discussion: Setup closures

Michel Fortin michel.fortin at michelf.ca
Fri Dec 4 15:51:42 CST 2015

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

More information about the swift-evolution mailing list