'if let where' alternative

36 views
Skip to first unread message

Bruno Berisso

unread,
Nov 20, 2015, 10:14:13 AM11/20/15
to Swift Language
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?



Chris Lattner

unread,
Nov 20, 2015, 12:58:22 PM11/20/15
to Bruno Berisso, Swift Language
Hi Bruno,

You should be able to use:

if unrelatedCondition, let unwrappedValue = someExpensiveOperation() {
}

-Chris

Bruno Berisso

unread,
Nov 20, 2015, 2:58:03 PM11/20/15
to Chris Lattner, Swift Language
Right,

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

Thanks
Reply all
Reply to author
Forward
0 new messages