<div>Assuming capture were allowed, people defining classes within functions who didn&#39;t want them to capture could position the class definition prior to any other code in the function so that there would be nothing to capture. </div><div><br><div class="gmail_quote"><div>On Thu, Dec 22, 2016 at 4:13 PM Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have to agree with Michael; it seems dangerous to allow implicit capture by classes. A primary purpose (telos?) of closures is to provide this functionality, which is actually quite an advanced concept. One knows to think carefully about this when encountering a closure expression. A primary purpose of classes is to provide for encapsulation of code. Accidentally extending the lifetime of a local variable in a containing scope would be hard to notice and highly unexpected functionality. Better not to mix these things.<br class="gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg">On Thu, Dec 22, 2016 at 17:54 Micah Hainline via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">That&#39;s exactly what I&#39;m suggesting, the class declaration could work similarly to a closure.<br class="gmail_msg"><br><br><br class="gmail_msg"><br><br>&gt; On Dec 22, 2016, at 4:15 PM, Michael Ilseman &lt;<a href="mailto:milseman@apple.com" class="gmail_msg" target="_blank">milseman@apple.com</a>&gt; wrote:<br class="gmail_msg"><br><br>&gt;<br class="gmail_msg"><br><br>&gt; Are you asking for a class declaration to implicitly capture and extend the lifetime of local variables? That seems like something that’s better done explicitly. Perhaps it’s better to think about how to reduce the boiler plate code, e.g. better default initializers.<br class="gmail_msg"><br><br>&gt;<br class="gmail_msg"><br><br>&gt; (this is of course, additive and beyond the current scope of Swift 4 phase 1 planning)<br class="gmail_msg"><br><br>&gt;<br class="gmail_msg"><br><br>&gt;&gt; On Dec 22, 2016, at 2:39 PM, Micah Hainline via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt; Currently we allow declarations of a new class in local scope, but<br class="gmail_msg"><br><br>&gt;&gt; nothing can be referenced from the outer scope. Thus this is illegal:<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt; ```<br class="gmail_msg"><br><br>&gt;&gt; func foo() {<br class="gmail_msg"><br><br>&gt;&gt;  let widgetString: String = createWidgetString()<br class="gmail_msg"><br><br>&gt;&gt;  class SimpleProvider: WidgetStringProvider {<br class="gmail_msg"><br><br>&gt;&gt;     func provideWidgetString() -&gt; String {<br class="gmail_msg"><br><br>&gt;&gt;        return widgetString // Illegal, defined in outer scope<br class="gmail_msg"><br><br>&gt;&gt;     }<br class="gmail_msg"><br><br>&gt;&gt;  }<br class="gmail_msg"><br><br>&gt;&gt;  doThingsWithWidget(provider: WidgetStringProvider())<br class="gmail_msg"><br><br>&gt;&gt; }<br class="gmail_msg"><br><br>&gt;&gt; ```<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt; I&#39;m trying to feel out the edges of this decision, and figure out why<br class="gmail_msg"><br><br>&gt;&gt; exactly this isn&#39;t allowed now, and how we might want to relax this in<br class="gmail_msg"><br><br>&gt;&gt; the future if possible. While not a common construct, it is a useful<br class="gmail_msg"><br><br>&gt;&gt; one.<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt; Obviously there are ways around it, very simple to create an init and<br class="gmail_msg"><br><br>&gt;&gt; a private let and do something like this:<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt; ```<br class="gmail_msg"><br><br>&gt;&gt; func foo() {<br class="gmail_msg"><br><br>&gt;&gt;  let widgetString: String = createWidgetString()<br class="gmail_msg"><br><br>&gt;&gt;  class SimpleProvider: WidgetStringProvider {<br class="gmail_msg"><br><br>&gt;&gt;     private let widgetString: String<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt;     init(widgetString: String) {<br class="gmail_msg"><br><br>&gt;&gt;        self.widgetString = widgetString<br class="gmail_msg"><br><br>&gt;&gt;     }<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt;     func provideWidgetString() -&gt; String {<br class="gmail_msg"><br><br>&gt;&gt;        return widgetString // now legal, references<br class="gmail_msg"><br><br>&gt;&gt; SimpleProvider.widgetString<br class="gmail_msg"><br><br>&gt;&gt;     }<br class="gmail_msg"><br><br>&gt;&gt;  }<br class="gmail_msg"><br><br>&gt;&gt;  doThingsWithWidget(provider: WidgetStringProvider(widgetString:<br class="gmail_msg"><br><br>&gt;&gt; widgetString))<br class="gmail_msg"><br><br>&gt;&gt; }<br class="gmail_msg"><br><br>&gt;&gt; ```<br class="gmail_msg"><br><br>&gt;&gt;<br class="gmail_msg"><br><br>&gt;&gt; That&#39;s boilerplate I don&#39;t want to write though, as it somewhat<br class="gmail_msg"><br><br>&gt;&gt; detracts from the readability of the structure. I&#39;m not super<br class="gmail_msg"><br><br>&gt;&gt; interested in defending the concept of local class definitions itself,<br class="gmail_msg"><br><br>&gt;&gt; it&#39;s already allowed in the language, I&#39;m just interested in the<br class="gmail_msg"><br><br>&gt;&gt; syntax limitation and where that line might be able to be redrawn.<br class="gmail_msg"><br><br>&gt;&gt; _______________________________________________<br class="gmail_msg"><br><br>&gt;&gt; swift-evolution mailing list<br class="gmail_msg"><br><br>&gt;&gt; <a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><br><br>&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"><br><br>&gt;<br class="gmail_msg"><br><br>_______________________________________________<br class="gmail_msg"><br><br>swift-evolution mailing list<br class="gmail_msg"><br><br><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><br><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"><br><br></blockquote></div><br><br>_______________________________________________<br class="gmail_msg"><br>swift-evolution mailing list<br class="gmail_msg"><br><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"><br></blockquote></div></div>