[swift-evolution] Shorthand unwrap proposal

Charlie Monroe charlie at charliemonroe.net
Fri Jun 24 07:50:45 CDT 2016


Yes, this is a bit different. There was a discussion about a month ago (http://thread.gmane.org/gmane.comp.lang.swift.evolution/17142 <http://thread.gmane.org/gmane.comp.lang.swift.evolution/17142>) which had a few good ideas how to deal with the following pattern:

if let x = x { // do something with x }
guard let x = x { return }

which is shadowing the original optional value. The suggestion was:

if let x! { // within this block, x is no longer optional }

guard let x! { return }
// Now x is no longer an optional.

Generally, it re-used the same variable name to safe-unwrap the optional. In your particular example:

let i: Int? = nil
if let i! {
	let y = i.toIntMax()
	/// ...
}

I am aware of the .map (or flatMap) on the optional, however, the guard statement in particular seems like an improvement.

There are many language constructs in Swift that can be expressed using other constructs - you can go all the way down and say that you really don't need anything other than if and goto.

The question is whether such a syntax sugar is something that would be help one write safer code and if it's something people would use.


> On Jun 23, 2016, at 9:32 PM, David Sweeris <davesweeris at mac.com> wrote:
> 
> Dmitri pointed out a few posts ago that Swift already has this.
> let opInt: Int? = nil
> opInt.map {$0.toIntMax()} //Evaluates to nil
> 
> Are you talking about something different?
> 
> - Dave Sweeris
> 
>> On Jun 23, 2016, at 2:04 PM, Charlie Monroe via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Sure, the exact syntax is a matter of discussion, I just wasn't that much of favor of the very short
>> 
>> doSomething(with: myOptional?)
>> 
>> - it looks like a great idea, making the code really short
>> - on the other hand the question mark is next to the variable, but the method's execution is optional - in that sense something like doSomething(?: myOptional)(with: myOptional) makes more sense, declaring explicitely what optionals does the execution depend on.
>> - nevertheless, in the interest of clarity and readability of the code, I'm still in favor of the original proposal, which requires you to either use if or guard.
>> 
>>> On Jun 23, 2016, at 8:57 PM, Tim Vermeulen <tvermeulen at me.com <mailto:tvermeulen at me.com>> wrote:
>>> 
>>> But ! still suggests force unwrapping, while ? suggests safe unwrapping. Why not use a question mark?
>>> 
>>>> It was in the previous proposal and suggested that you are not trying to shadow the previous variable, but trying to unwrap it - and it acts as unwrapped from there on.
>>>> 
>>>> 
>>>>> On Jun 23, 2016, at 8:52 PM, Tim Vermeulen<tvermeulen at me.com <mailto:tvermeulen at me.com>>wrote:
>>>>> 
>>>>> Why with the exclamation mark? It suggests you’re force unwrapping something.
>>>>> 
>>>>>>> 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) }...
>>>>>> 
>>>>>>> 
>>>>>>> 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 <mailto:james at supmenow.com>)⎥supmenow.com <http://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
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160624/c657fd32/attachment.html>


More information about the swift-evolution mailing list