[swift-evolution] Shorthand unwrap proposal

Tim Vermeulen tvermeulen at me.com
Thu Jun 23 14:09:12 CDT 2016


I wasn’t suggesting we allow the use of optional values as function parameters and just do nothing if the parameter is indeed optional. Optional parameters can be perfectly valid. I was talking about a syntax where we can put ? behind an optional parameter to signify that we don’t want to execute the function in case that particular parameter turns out to be nil, and we want the function to return nil if it has a return value.

> Regards(Frommobile)
> 
> On Jun 23, 2016, at 8:51 PM, Charlie Monroe via swift-evolution<swift-evolution at swift.org(mailto:swift-evolution at swift.org)>wrote:
> 
> > 
> > > On Jun 23, 2016, at 8:45 PM, Tim Vermeulen via swift-evolution<swift-evolution at swift.org(mailto:swift-evolution at swift.org)>wrote:
> > > 
> > > I would love to be able to do something like
> > > 
> > > doSomething(with: myOptional?)
> > 
> > This actually looks good to me, though if I were a newcomer to the language, it would be really cryptic.
> > 
> > In case the function returned any value, it could become an optional, just like with try?...
> > 
> > I still, however, prefer the original proposal of if let myOptional! { doSomething(myOptional) }...
> > 
> func doSomething<T>(with: t:T?, h:T->()) {
> iflett = self as?T{ h(t) }
> }
> extension Optional {
> 
> 
> funcunwrap<T>(_h:T->()) {
> 
> iflett = self as?T{ h(t) }
> }
> 
> 
> }
> 
> Var s2:Sting?
> s2.unwrap{ (str:String)in
> print(":)")
> }
> 
> 
> doSomething(with:s2) { //same as above}
> 
> Nothing happens in both cases
> 
> > > 
> > > which would be equivalent to
> > > 
> > > if let myValue = myOptional {
> > > doSomething(with: myValue)
> > > }
> > > 
> > > But it’s been discussed here before, and I don’t think people were very enthusiastic about it.
> > > 
> > > > 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(mailto:james at supmenow.com)(mailto:james at supmenow.com)⎥supmenow.com(http://supmenow.com)(http://supmenow.com)
> > > > 
> > > > 
> > > > Sup
> > > > 
> > > > 
> > > > Runway East
> > > > 
> > > > 
> > > > 10 Finsbury Square
> > > > 
> > > > 
> > > > London
> > > > 
> > > > 
> > > > EC2A 1AF
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > _______________________________________________
> > > swift-evolution mailing list
> > > swift-evolution at swift.org(mailto:swift-evolution at swift.org)
> > > https://lists.swift.org/mailman/listinfo/swift-evolution
> > 
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org(mailto:swift-evolution at swift.org)
> > https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> 


More information about the swift-evolution mailing list