[swift-evolution] Shorthand unwrap proposal

Sean Heber sean at fifthace.com
Thu Jun 23 11:11:05 CDT 2016


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:

myReallyLongOptionalName.unwrap { string in
  doSomethingWith(string)
}

And that is actually *more* characters than the current approach:

if let string = myReallyLongOptionalName {
  doSomethingWith(string)
}

However it’d be a big win especially when you can skip $0 and the braces entirely such as:

myReallyLongOptionalName.unwrap(doSomethingWith)

Of course if we were dealing with methods, you could write this like:

myReallyLongOptionalName?.doSomething()

And that is probably hard to beat.

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.

I don’t have a solution - just trying to clarify the situation. :)

l8r
Sean


> On Jun 23, 2016, at 10:36 AM, James Campbell via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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.
> 
> So this:
> 
> let myReallyLongOptionalName: String? = "Hey"
> 
> if let string = myReallyLongOptionalName {
>   doSomethingWith(string)
> }
> 
> Could become"
> 
> let myReallyLongOptionalName: String? = "Hey"
> 
> myReallyLongOptionalName.unwrap {
>   doSomethingWith($0)
> }
> 
> The block would only be fired if myReallyLongOptionalName has a value.
> 
> ___________________________________
> 
> James⎥Head of Trolls
> 
> james at supmenow.com⎥supmenow.com
> 
> Sup
> 
> Runway East
> 
> 
> 10 Finsbury Square
> 
> London
> 
> 
> EC2A 1AF 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list