'if let where' alternative

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

'if let where' alternative

Bruno Berisso
Hi all.

I'm facing this kind of expressions quite frequently:

if let unwrappedValue = someExpensiveOperation() where unrelatedCondition {
   
...
}

This kind of if let expression always executes someExpensiveOperation before check the where clause. The obvious approach here is move the unrelatedCondition to an external if like:

if unrelatedCondition {
   
if let unwrappedValue = someExpensiveOperation() {
       
...
   
}
}

There is another way to write this?



--
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/916b0d89-a834-46d0-87f2-fb4924ed4990%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: 'if let where' alternative

Chris Lattner

On Nov 20, 2015, at 7:14 AM, Bruno Berisso <[hidden email]> wrote:

Hi all.

I'm facing this kind of expressions quite frequently:

if let unwrappedValue = someExpensiveOperation() where unrelatedCondition {
   
...
}

This kind of if let expression always executes someExpensiveOperation before check the where clause. The obvious approach here is move the unrelatedCondition to an external if like:

if unrelatedCondition {
   
if let unwrappedValue = someExpensiveOperation() {
       
...
   
}
}

There is another way to write this?


Hi Bruno,

You should be able to use:

if unrelatedCondition, let unwrappedValue = someExpensiveOperation() {
}

-Chris

--
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/9C176B42-3365-4C1D-ADC0-883AFAD3DBB5%40apple.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: 'if let where' alternative

Bruno Berisso
Right,

I was trying the same with '&&' instead of ','

Thanks

El vie., 20 de nov. de 2015 a la(s) 2:58 p. m., Chris Lattner <[hidden email]> escribió:

On Nov 20, 2015, at 7:14 AM, Bruno Berisso <[hidden email]> wrote:

Hi all.

I'm facing this kind of expressions quite frequently:

if let unwrappedValue = someExpensiveOperation() where unrelatedCondition {
   
...
}

This kind of if let expression always executes someExpensiveOperation before check the where clause. The obvious approach here is move the unrelatedCondition to an external if like:

if unrelatedCondition {
   
if let unwrappedValue = someExpensiveOperation() {
       
...
   
}
}

There is another way to write this?


Hi Bruno,

You should be able to use:

if unrelatedCondition, let unwrappedValue = someExpensiveOperation() {
}

-Chris

--
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/CA%2BaYCtft-oP_W63MaQu6AOZa%2BR%3Du1EH1c19ukOJVW1hx072pKA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.