optional binding if let x = x

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

optional binding if let x = x

Boon Chew
Hi all,

I am seeing increasing use of this idiom in optional binding:

if let x = x {
  ...
}

Is this a best practice or established idiom? While I can see the value of not having to think about another name for the unwrapped variable, it seems wrong to be changing switching the type of the variable under the hood while treating it as if it is the original (i.e. from optional to unwrapped).  What if you inadvertently try to unwrap it in a long method body, not knowing the Optionality is already stripped?

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8d864ed5-e49f-481b-abd6-6a7a981d1513%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: optional binding if let x = x

Jim Dovey
On Jun 17, 2015, at 10:55 AM, Boon <[hidden email]> wrote:

Hi all,

I am seeing increasing use of this idiom in optional binding:

if let x = x {
  ...
}

Is this a best practice or established idiom? While I can see the value of not having to think about another name for the unwrapped variable, it seems wrong to be changing switching the type of the variable under the hood while treating it as if it is the original (i.e. from optional to unwrapped).  What if you inadvertently try to unwrap it in a long method body, not knowing the Optionality is already stripped?

The idea is that the code within the braces here is only executed when x is non-nil. The purpose of the if-let in this case is to explicitly remove the need for manually unwrapping the variable where it is being used, while still retaining its name for simplicity and ease of recognition. It’s perfectly acceptable to use another name, as well, but you can think of this construct as being similar to ‘if x is not nil, then [do stuff with x]’. In that case, keeping the same name makes that intention clear.

Note also that the guard keyword allows you to mark a function as accepting nil, but that it only performs operations if that variable is non-nil. Using ‘guard let x = x else { return }’ allows the method to be written as if x was non-optional while also allowing for a nil input that would simply be a no-op.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/96820BE5-D52B-4D9B-B2B0-2BE8DF824B34%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: optional binding if let x = x

Boon Chew
Really like the way you explained it, thank you.

On Wednesday, June 17, 2015 at 2:04:24 PM UTC-4, Jim Dovey wrote:
On Jun 17, 2015, at 10:55 AM, Boon <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="O4SqLGSRdbMJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">bo...@...> wrote:

Hi all,

I am seeing increasing use of this idiom in optional binding:

if let x = x {
  ...
}

Is this a best practice or established idiom? While I can see the value of not having to think about another name for the unwrapped variable, it seems wrong to be changing switching the type of the variable under the hood while treating it as if it is the original (i.e. from optional to unwrapped).  What if you inadvertently try to unwrap it in a long method body, not knowing the Optionality is already stripped?

The idea is that the code within the braces here is only executed when x is non-nil. The purpose of the if-let in this case is to explicitly remove the need for manually unwrapping the variable where it is being used, while still retaining its name for simplicity and ease of recognition. It’s perfectly acceptable to use another name, as well, but you can think of this construct as being similar to ‘if x is not nil, then [do stuff with x]’. In that case, keeping the same name makes that intention clear.

Note also that the guard keyword allows you to mark a function as accepting nil, but that it only performs operations if that variable is non-nil. Using ‘guard let x = x else { return }’ allows the method to be written as if x was non-optional while also allowing for a nil input that would simply be a no-op.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/4d55955b-144d-4782-8909-fc75d12abdda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: optional binding if let x = x

Marco S Hyman
In reply to this post by Boon Chew
> What if you inadvertently try to unwrap it in a long method body, not knowing the Optionality is already stripped?

The unwrapped is not optional.  Trying to unwrap in will generate a syntax
error.

Try this:

let x: Int? = 100

if let x = x {
    let y = x!
}

The compiler will tell you that ! requires an optional type.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/D178777E-75B0-4F37-8AC0-6F7E22017CCD%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: optional binding if let x = x

Boon Chew
Thanks Marco, good point.

On Wednesday, June 17, 2015 at 3:51:38 PM UTC-4, Marco Hyman wrote:
> What if you inadvertently try to unwrap it in a long method body, not knowing the Optionality is already stripped?

The unwrapped is not optional.  Trying to unwrap in will generate a syntax
error.

Try this:

let x: Int? = 100

if let x = x {
    let y = x!
}

The compiler will tell you that ! requires an optional type.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8e9a2043-e40e-4320-9817-4664432368eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.