<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 25, 2016, at 3:48 PM, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I like this pretty well, and I think "with()" makes sense as a peer of "withUnsafePointer()", "withExtendedLifetime()", etc.<div class=""><br class=""></div><div class="">I'd also be okay with waiting for a comprehensive method-cascading solution. I don't find this issue particularly urgent, because it's pretty easily solvable with an extension or just using closures.</div></div></div></blockquote><div><br class=""></div><div>+1. &nbsp;I’ve been playing around with it in my own code a little bit. &nbsp;I’m still uncertain about when I think it is good style and when I think it is best avoided. &nbsp;I would certainly feel more comfortable using it if it was in the standard library. &nbsp;</div><div><br class=""></div><div>At the same time, I think we can and should do better in the future. &nbsp;If that is the plan, do we really want `with` in the standard library? &nbsp;I don’t mind waiting for a better solution, especially if it means a better solution is more likely to happen and / or we aren’t left with an unnecessary and duplicative function in the standard library.</div><div><br class=""></div><div>So I’m on the fence here. &nbsp;</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra">
<br class=""><div class="gmail_quote">On Wed, May 25, 2016 at 11:28 AM, Erica Sadun via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Over the past couple of days, the Twitters have discovered some work I'd done on closure-based setup.&nbsp;<div class="">It's clear that a demand is out there and strong for this kind of behavior, even without implicit `self` as&nbsp;</div><div class="">part of the mix or cascading. In that light, I've put together the following:<div class=""><br class=""></div><div class=""><a href="https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae" target="_blank" class="">https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae</a></div><div class=""><br class=""></div><div class="">If the community demand is this high, I think we should re-consider pushing it before 3.&nbsp;</div><div class="">Feedback as always welcome, including criticism.</div><div class=""><br class=""></div><div class="">-- E</div><div class=""><br class=""></div><div class=""><h1 style="font-size:2.25em;margin-right:0px;margin-bottom:16px;margin-left:0px;line-height:1.2;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255);margin-top:0px!important" class="">Introducing&nbsp;<code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">with</code>&nbsp;to the Standard Library</h1><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><li class="">Proposal: TBD</li><li class="">Author:&nbsp;<a href="https://github.com/erica" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank" class="">Erica Sadun</a></li><li class="">Status: TBD</li><li class="">Review manager: TBD</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a href="https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae#introduction" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank" class=""><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Introduction</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal introduces a&nbsp;<code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">with</code>&nbsp;function to the standard library to simplify the initialization and modification of constants and Foundation-sourced complex objects.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Swift-evolution thread:&nbsp;<a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/14384" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank" class="">What about a VBA style with Statement?</a></p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a href="https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae#motivation" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank" class=""><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Motivation</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Closure-based initialization enables clean and highly directed set-up in Swift code. Numerous variations on the theme have been introduced on the Swift Evolution list and in third party github repositories. Although you can build solutions natively without functions, current Swift technology has drawbacks:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><pre style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal" class=""><span style="color:rgb(167,29,93)" class="">let</span> questionLabel: UILabel <span style="color:rgb(167,29,93)" class="">=</span> {
    $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>textAlignment <span style="color:rgb(167,29,93)" class="">=</span> <span style="color:rgb(167,29,93)" class="">.</span>Center
    $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>font <span style="color:rgb(167,29,93)" class="">=</span>  UIFont(name:<span style="color:rgb(24,54,145)" class=""><span class="">"</span>DnealianManuscript<span class="">"</span></span>, size: <span style="color:rgb(0,134,179)" class="">72</span>)
    $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>text <span style="color:rgb(167,29,93)" class="">=</span> questionText
    $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>numberOfLines <span style="color:rgb(167,29,93)" class="">=</span> <span style="color:rgb(0,134,179)" class="">0</span>
    <span style="color:rgb(167,29,93)" class="">return</span> $<span style="color:rgb(0,134,179)" class="">0</span>
}(UILabel())

<span style="color:rgb(167,29,93)" class="">let</span> mySwitch <span style="color:rgb(167,29,93)" class="">:</span> UISwitch <span style="color:rgb(167,29,93)" class="">=</span> {
    view<span style="color:rgb(167,29,93)" class="">.</span>addSubview($<span style="color:rgb(0,134,179)" class="">0</span>)
    CenterViewInSuperview($<span style="color:rgb(0,134,179)" class="">0</span>, horizontal: <span style="color:rgb(0,134,179)" class="">true</span>, vertical: <span style="color:rgb(0,134,179)" class="">true</span>)
    $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>addTarget(<span style="color:rgb(167,29,93)" class="">self</span>, action: <span style="color:rgb(24,54,145)" class=""><span class="">"</span>action<span class="">"</span></span>, forControlEvents: <span style="color:rgb(167,29,93)" class="">.</span>TouchUpInside)
    <span style="color:rgb(167,29,93)" class="">return</span> $<span style="color:rgb(0,134,179)" class="">0</span>
}(UISwitch())</pre></div><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><li class="">Assignment must be explicitly typed.</li><li class="">The source item must be postpended to the set-up closure.</li><li class="">The closure must return the item.</li><li class="">This approach is better suited to setting up Foundation objects than modifying Swift constants. When duplicating and modifying a constant, the closure must create a var copy and modify that copy.</li></ul><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">While the implementation is imperfect, the wins are notable. Code naturally groups into a clear set-up sequence. The scoped setup avoids clumpy redundant lines where the same variable is accessed over and over.&nbsp;</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><pre style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal" class=""><span style="color:rgb(167,29,93)" class="">let</span> questionLabel <span style="color:rgb(167,29,93)" class="">=</span> UILabel()
questionLabel<span style="color:rgb(167,29,93)" class="">.</span>textAlignment <span style="color:rgb(167,29,93)" class="">=</span> <span style="color:rgb(167,29,93)" class="">.</span>Center
questionLabel<span style="color:rgb(167,29,93)" class="">.</span>font <span style="color:rgb(167,29,93)" class="">=</span>  UIFont(name:<span style="color:rgb(24,54,145)" class=""><span class="">"</span>DnealianManuscript<span class="">"</span></span>, size: <span style="color:rgb(0,134,179)" class="">72</span>)
questionLabel<span style="color:rgb(167,29,93)" class="">.</span>text <span style="color:rgb(167,29,93)" class="">=</span> questionText
questionLabel<span style="color:rgb(167,29,93)" class="">.</span>numberOfLines <span style="color:rgb(167,29,93)" class="">=</span> <span style="color:rgb(0,134,179)" class="">0</span></pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">In the case of non-reference types, a constant's fields may be set-up sequentially without forcing the constant to be a variable.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a href="https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae#detailed-design" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank" class=""><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Detailed Design</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal introduces a&nbsp;<code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">with</code>&nbsp;function that enables modification and use of an instance using positional references. It's not quite as clean as a solution with implicit&nbsp;<code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">self</code>&nbsp;but it offers sufficient utility that a vast swath of Swift developers have adopted this function in some form or another.</p><pre style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;color:rgb(51,51,51)" class=""><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;padding:0px;margin:0px;background-color:transparent;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal" class="">@discardableResult
public func with&lt;T&gt;(_ item: T, update: @noescape (inout T) throws -&gt; Void) rethrows -&gt; T {
    var this = item; try update(&amp;this); return this
}
</code></pre><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">In use:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><pre style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal" class=""><span style="color:rgb(167,29,93)" class="">struct</span> Person { <span style="color:rgb(167,29,93)" class="">var</span> name: <span style="color:rgb(0,134,179)" class="">String</span>, favoriteColor: UIColor }
<span style="color:rgb(167,29,93)" class="">let</span> john <span style="color:rgb(167,29,93)" class="">=</span> Person(name: <span style="color:rgb(24,54,145)" class=""><span class="">"</span>John<span class="">"</span></span>, favoriteColor: <span style="color:rgb(167,29,93)" class="">.</span>blueColor())
<span style="color:rgb(167,29,93)" class="">let</span> jane <span style="color:rgb(167,29,93)" class="">=</span> with(john){ $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>name <span style="color:rgb(167,29,93)" class="">=</span> <span style="color:rgb(24,54,145)" class=""><span class="">"</span>Jane<span class="">"</span></span> }
<span style="color:rgb(0,134,179)" class="">print</span>(jane) <span style="color:rgb(150,152,150)" class="">// Person(name: "Jane", favoriteColor: UIDeviceRGBColorSpace 0 0 1 1)</span>

<span style="color:rgb(167,29,93)" class="">struct</span> Point { <span style="color:rgb(167,29,93)" class="">var</span> (x, y) <span style="color:rgb(167,29,93)" class="">:</span> (<span style="color:rgb(0,134,179)" class="">Double</span>, <span style="color:rgb(0,134,179)" class="">Double</span>) }
<span style="color:rgb(167,29,93)" class="">let</span> p1 <span style="color:rgb(167,29,93)" class="">=</span> Point(x: <span style="color:rgb(0,134,179)" class="">2</span>, y: <span style="color:rgb(0,134,179)" class="">3</span>)
<span style="color:rgb(167,29,93)" class="">let</span> p2 <span style="color:rgb(167,29,93)" class="">=</span> with(p1){ $<span style="color:rgb(0,134,179)" class="">0</span><span style="color:rgb(167,29,93)" class="">.</span>y <span style="color:rgb(167,29,93)" class="">=</span> <span style="color:rgb(0,134,179)" class="">4</span> }
<span style="color:rgb(0,134,179)" class="">print</span>(p1, p2) <span style="color:rgb(150,152,150)" class="">// Point(x: 2.0, y: 3.0) Point(x: 2.0, y: 4.0)</span></pre></div><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a href="https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae#impact-on-existing-code" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank" class=""><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Impact on Existing Code</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal is purely additive and has no impact on existing code</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a href="https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae#alternatives-considered" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank" class=""><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Alternatives Considered</h2><div style="margin-top:0px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255);margin-bottom:0px!important" class="">Not adopting this proposal</div></div></div></div><br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>