<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=""><div class="">I think we’d still just recommend using ‘map’ for this. The reason Collection.map and Collection.forEach are different is because we don’t promise eager and in-order evaluation for Collection.map. But Optional only executes the closure one or zero times, so there’s no ambiguity.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 23, 2016, at 09:15, James Campbell 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=""><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;">So I have a real-life situation in an application, which does what you mention:</div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;">This code is for a camera app, on a `didSet` it removes a device if set from the capture session, and if there is a new one set it adds it to the capture session.</div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;">The add and remove methods indeed don't take optionals.</div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;">So this is the code before:</div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><p class=""><span class="">var</span><span class=""> audioDevice: </span><span class="">AVCaptureDeviceInput</span><span class="">? = </span><span class="">nil</span><span class=""> {</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">willSet</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if let&nbsp;</span>audioDevice = audioDevice {</p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="">captureSession?.removeInput(</span>audioDevice)</p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">didSet</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp;</span>audioDevice = audioDevice {</p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="">captureSession?.addInput(</span>audioDevice)</p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;">and after:</div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><br class=""></div><div class="gmail_default" style="font-family: arial, helvetica, sans-serif; font-size: small;"><p class=""><span class="">var</span><span class=""> audioDevice: </span><span class="">AVCaptureDeviceInput</span><span class="">? = </span><span class="">nil</span><span class=""> {</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">willSet</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioDevice.unwrap {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.captureSession?.removeInput($0)</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">didSet</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioDevice.unwrap {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.captureSession?.addInput($0)</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class=""><br class=""></span></p><p class=""><span class="">The last two saved me a lot of typing in these cases and I feel like it is more clear what is going on due to the `unwrap` method being clear in it's intent and the lack of `</span>audioDevice` being repeated multiple times.</p></div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><p class=""><b class=""><font color="#cc0000" class="">___________________________________</font></b></p><p class=""><b class="">James⎥Head of Trolls</b></p><p class=""><b class=""><font color="#cc0000" class=""><a href="mailto:james@supmenow.com" target="_blank" class="">james@supmenow.com</a>⎥<a href="http://supmenow.com/" target="_blank" class="">supmenow.com</a></font></b></p><p class=""><b class=""><font size="2" class="">Sup</font></b></p><p class=""><b class=""><font size="2" class="">Runway East
</font></b></p><p class=""><b class=""><font size="2" class="">10 Finsbury Square</font></b></p><p class=""><b class=""><font size="2" class="">London</font></b></p><p class=""><b class=""><font size="2" class="">
EC2A 1AF&nbsp;</font></b></p></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br class=""><div class="gmail_quote">On 23 June 2016 at 17:11, Sean Heber <span dir="ltr" class="">&lt;<a href="mailto:sean@fifthace.com" target="_blank" class="">sean@fifthace.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I’m a bit tore on this myself. I see the appeal, but let’s say we had such a function. If you wanted to use it with an named parameter it’d look like this:<br class="">
<br class="">
myReallyLongOptionalName.unwrap { string in<br class="">
&nbsp; doSomethingWith(string)<br class="">
}<br class="">
<br class="">
And that is actually *more* characters than the current approach:<br class="">
<span class=""><br class="">
if let string = myReallyLongOptionalName {<br class="">
&nbsp; doSomethingWith(string)<br class="">
}<br class="">
<br class="">
</span>However it’d be a big win especially when you can skip $0 and the braces entirely such as:<br class="">
<br class="">
myReallyLongOptionalName.unwrap(doSomethingWith)<br class="">
<br class="">
Of course if we were dealing with methods, you could write this like:<br class="">
<br class="">
myReallyLongOptionalName?.doSomething()<br class="">
<br class="">
And that is probably hard to beat.<br class="">
<br class="">
So I think the problem really only presents itself when you have an optional that you need to unwrap and use as a parameter to something that does not take an optional.<br class="">
<br class="">
I don’t have a solution - just trying to clarify the situation. :)<br class="">
<br class="">
l8r<br class="">
<span class="HOEnZb"><font color="#888888" class="">Sean<br class="">
</font></span><div class="HOEnZb"><div class="h5"><br class="">
<br class="">
&gt; On Jun 23, 2016, at 10:36 AM, James Campbell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; I was wondering if people would be open to adding an unwrap method to the Optional type,&nbsp; I already have a method like this which shortens code for me.<br class="">
&gt;<br class="">
&gt; So this:<br class="">
&gt;<br class="">
&gt; let myReallyLongOptionalName: String? = "Hey"<br class="">
&gt;<br class="">
&gt; if let string = myReallyLongOptionalName {<br class="">
&gt;&nbsp; &nbsp;doSomethingWith(string)<br class="">
&gt; }<br class="">
&gt;<br class="">
&gt; Could become"<br class="">
&gt;<br class="">
&gt; let myReallyLongOptionalName: String? = "Hey"<br class="">
&gt;<br class="">
&gt; myReallyLongOptionalName.unwrap {<br class="">
&gt;&nbsp; &nbsp;doSomethingWith($0)<br class="">
&gt; }<br class="">
&gt;<br class="">
&gt; The block would only be fired if myReallyLongOptionalName has a value.<br class="">
&gt;<br class="">
&gt; ___________________________________<br class="">
&gt;<br class="">
&gt; James⎥Head of Trolls<br class="">
&gt;<br class="">
&gt; <a href="mailto:james@supmenow.com" class="">james@supmenow.com</a>⎥<a href="http://supmenow.com/" rel="noreferrer" target="_blank" class="">supmenow.com</a><br class="">
&gt;<br class="">
&gt; Sup<br class="">
&gt;<br class="">
&gt; Runway East
&gt;<br class="">
&gt;<br class="">
&gt; 10 Finsbury Square<br class="">
&gt;<br class="">
&gt; London<br class="">
&gt;<br class="">
&gt; &gt; EC2A 1AF<br class="">
&gt;<br class="">
</div></div><div class="HOEnZb"><div class="h5">&gt; _______________________________________________<br class="">
&gt; swift-evolution mailing list<br class="">
&gt; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
&gt; <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="">
</div></div></blockquote></div><br class=""></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>