<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Coming from Smalltalk where there are no native control structures at all, i.e. all control structures are built using closures, I'm very used to creating control structure-like methods or mini DSLs. For these trailing closures are indispensable.</div><div>That is also very common in other languages like Ruby or Scala.</div><div><br></div><div>(Smalltalk was even better because it has no parenthesis around argument lists which allowed for multiple closure arguments looking as nice as trailing closures.)</div><div><br></div><div>-Thorsten </div><div><br></div><div>Am 24.03.2016 um 14:57 schrieb Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">When I started using Swift I was initially very enthusiastic about trailing closures, but I’ve actually kind of gone off them somewhat and I’d like to discuss why.<div class=""><br class=""></div><div class="">Firstly, here are two ways to write a common example using the .map() method:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo = myArray.map { $0 + 1 }</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo = myArray.map({ $0 + 1 })</font></div><div class=""><br class=""></div><div class="">It’s tough to say that the first form is any neater than the second, other than the second having more brackets. However, the first form is somewhat ambiguous, as .map in this case looks like a property rather than a method, it also visually looks like a statement, followed by a closure rather than the two things logically being related. Of course it’s quick to learn that these are related, but for consistency I’m starting to now prefer the use of parenthesis in almost all cases.</div><div class=""><br class=""></div><div class="">The other advantage of trailing closures is the omission of the label, but trailing closures aren’t strictly necessary for this, as we can already omit external labels for parameters if we want to, and the example above shows that a trailing closure isn’t necessary for this. The only real difference is that the trailing closure form makes a label optional, because you can either provide the closure with label in parenthesis (if the label is required) or omit it by trailing, like so:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>something.someMethod(foo: 1, predicate: { $0 < $1})</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>something.someMethod(foo: 1) { $0 < $1}</font></div><div class=""><br class=""></div><div class="">However this kind of arbitrarily makes the following impossible:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>something.someMethod(foo: 1, { $0 < $1 })</font></div><div class=""><br class=""></div><div class="">With this in mind it seems to me that we might be better served by the ability to make external labels optional, as this would allow us to be just as succinct, while being completely clear about what is being passed into this method.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The only real remaining advantage that I see to trailing closures is the ability to define pseudo language constructs, for example:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func repeatUntilEmpty<C:CollectionType>(collection:C, @noescape _ body:() throws -> Void) rethrows { while !collection.isEmpty { body() } }</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></font><span style="font-family: Monaco;" class="">repeatUntilEmpty(myArray)</span><font face="Monaco" class=""> {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>/* Do something over and over until myArray is empty */</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><br class=""></div><div class="">Which I think is a pretty uncommon type of structure, but could be useful in some specialised situations. To support this though we could easily use a new @trailing attribute instead to indicate that the closure can be used in this way. My example isn’t very good as I can’t think of a case that really, really needs this, but I think they’re probably out there.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">To summarise, having come down off my initial enthusiasm for trailing closures I’m not sure that they really add that much syntactically, especially in the most common cases, while actually being a little ambiguous looking and adding inconsistency to the language. I think they should remain for the less common cases that can really benefit from them, but as a feature that is opted into, so that we can go for consistency by default.</div><div class=""><br class=""></div><div class="">I’m interested to hear other people’s thoughts.</div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>