<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">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;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">So this is the code before:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">
<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>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">willSet</span><span class=""> {</span></p>
<p class=""><span class=""> if let </span>audioDevice = audioDevice {</p>
<p class=""><span class=""> </span><span class="">captureSession?.removeInput(</span>audioDevice)</p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">didSet</span><span class=""> {</span></p>
<p class=""><span class=""> if </span>audioDevice = audioDevice {</p>
<p class=""><span class=""> </span><span class="">captureSession?.addInput(</span>audioDevice)</p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">and after:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(0,0,0)">
<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>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">willSet</span><span class=""> {</span></p>
<p class=""><span class=""> audioDevice.unwrap {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.captureSession?.removeInput($0)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">didSet</span><span class=""> {</span></p>
<p class=""><span class=""> audioDevice.unwrap {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.captureSession?.addInput($0)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p><p class=""><span class=""><br></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"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">
<p><b><font color="#cc0000">___________________________________</font></b></p><p><b>James⎥Head of Trolls</b></p><p><b><font color="#cc0000"><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>⎥<a href="http://supmenow.com" target="_blank">supmenow.com</a></font></b></p><p><b><font size="2">Sup</font></b></p><p><b><font size="2">Runway East
</font></b></p><p><b><font size="2">10 Finsbury Square</font></b></p><p><b><font size="2">London</font></b></p><p><b><font size="2">
EC2A 1AF </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><div class="gmail_quote">On 23 June 2016 at 17:11, Sean Heber <span dir="ltr"><<a href="mailto:sean@fifthace.com" target="_blank">sean@fifthace.com</a>></span> wrote:<br><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>
<br>
myReallyLongOptionalName.unwrap { string in<br>
doSomethingWith(string)<br>
}<br>
<br>
And that is actually *more* characters than the current approach:<br>
<span class=""><br>
if let string = myReallyLongOptionalName {<br>
doSomethingWith(string)<br>
}<br>
<br>
</span>However it’d be a big win especially when you can skip $0 and the braces entirely such as:<br>
<br>
myReallyLongOptionalName.unwrap(doSomethingWith)<br>
<br>
Of course if we were dealing with methods, you could write this like:<br>
<br>
myReallyLongOptionalName?.doSomething()<br>
<br>
And that is probably hard to beat.<br>
<br>
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>
<br>
I don’t have a solution - just trying to clarify the situation. :)<br>
<br>
l8r<br>
<span class="HOEnZb"><font color="#888888">Sean<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
> On Jun 23, 2016, at 10:36 AM, James Campbell via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
> I was wondering if people would be open to adding an unwrap method to the Optional type, I already have a method like this which shortens code for me.<br>
><br>
> So this:<br>
><br>
> let myReallyLongOptionalName: String? = "Hey"<br>
><br>
> if let string = myReallyLongOptionalName {<br>
> doSomethingWith(string)<br>
> }<br>
><br>
> Could become"<br>
><br>
> let myReallyLongOptionalName: String? = "Hey"<br>
><br>
> myReallyLongOptionalName.unwrap {<br>
> doSomethingWith($0)<br>
> }<br>
><br>
> The block would only be fired if myReallyLongOptionalName has a value.<br>
><br>
> ___________________________________<br>
><br>
> James⎥Head of Trolls<br>
><br>
> <a href="mailto:james@supmenow.com">james@supmenow.com</a>⎥<a href="http://supmenow.com" rel="noreferrer" target="_blank">supmenow.com</a><br>
><br>
> Sup<br>
><br>
> Runway East
><br>
><br>
> 10 Finsbury Square<br>
><br>
> London<br>
><br>
> > EC2A 1AF<br>
><br>
</div></div><div class="HOEnZb"><div class="h5">> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</div></div></blockquote></div><br></div>