if let ...

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

if let ...

Marco S Hyman
Both of the following code bits seem to do the same thing...

    if let foo = optionalFoo,
       let bar = optionalBar {
        // ...
    }

    if let foo = optionalFoo,
           bar = optionalBar {
        // ...
    }

Is there any reason why one should be preferred over the other?

--
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/DA2996E0-CB4F-4DFB-87D5-32C6B49FC233%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: if let ...

Chris Lattner

> On Jul 14, 2015, at 12:49 PM, Marco S Hyman <[hidden email]> wrote:
>
> Both of the following code bits seem to do the same thing...
>
>    if let foo = optionalFoo,
>       let bar = optionalBar {
>        // ...
>    }
>
>    if let foo = optionalFoo,
>           bar = optionalBar {
>        // ...
>    }
>
> Is there any reason why one should be preferred over the other?

You *must* use the former if you want to use a boolean condition between them with a "where" clause.  In the absence of a where clause, this is a style question.

IMHO, I'd use the former when wrapping the lines as you have, but use the later when both foo/bar are on the same line, e.g.:

  if let foo = optionalFoo, bar = optionalBar {
       // ...
   }

-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/BA41571C-0EC3-4287-BB25-4950520A3438%40apple.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: if let ...

Bruno Berisso
I try it in a playground and can't find a difference. This is the code:


let op1: Any? = "test"
let op2: Any? = 4

//Version 1
//
if let a = op1 as? String, 
        b = op2 as? Int 
   where a == "test" && b > 1 {
    "work"
} else {
    "don't work"
}

//Version 2
//
if let a = op1 as? String, 
   let b = op2 as? Int 
   where a == "test" && b > 1 {
    "work"
} else {
    "don't work"
}


This always "work"

2015-07-15 2:03 GMT-03:00 Chris Lattner <[hidden email]>:

> On Jul 14, 2015, at 12:49 PM, Marco S Hyman <[hidden email]> wrote:
>
> Both of the following code bits seem to do the same thing...
>
>    if let foo = optionalFoo,
>       let bar = optionalBar {
>        // ...
>    }
>
>    if let foo = optionalFoo,
>           bar = optionalBar {
>        // ...
>    }
>
> Is there any reason why one should be preferred over the other?

You *must* use the former if you want to use a boolean condition between them with a "where" clause.  In the absence of a where clause, this is a style question.

IMHO, I'd use the former when wrapping the lines as you have, but use the later when both foo/bar are on the same line, e.g.:

  if let foo = optionalFoo, bar = optionalBar {
       // ...
   }

-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/BA41571C-0EC3-4287-BB25-4950520A3438%40apple.com.
For more options, visit https://groups.google.com/d/optout.

--
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%2BaYCtegnFFJe%3Dh4wuOx_3p_zEToKph8dzYEm7BuMzWEBOtuyQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: if let ...

Jim Dovey
The difference is when you place a `where` clause *between* the assignments, i.e. before assigning `b`:

// This is valid
if let a = op1 as? String where !a.isEmpty,
let b = op2 as? String {
// work
}

// This is not
if let a = op1 as? String where !a.isEmpty,
b = op2 as? String { // ← missing `let`
// work
}

Cheers,
-Jim

On Jul 29, 2015, at 7:38 AM, Bruno Berisso <[hidden email]> wrote:

I try it in a playground and can't find a difference. This is the code:


let op1: Any? = "test"
let op2: Any? = 4

//Version 1
//
if let a = op1 as? String, 
        b = op2 as? Int 
   where a == "test" && b > 1 {
    "work"
} else {
    "don't work"
}

//Version 2
//
if let a = op1 as? String, 
   let b = op2 as? Int 
   where a == "test" && b > 1 {
    "work"
} else {
    "don't work"
}


This always "work"

2015-07-15 2:03 GMT-03:00 Chris Lattner <[hidden email]>:

> On Jul 14, 2015, at 12:49 PM, Marco S Hyman <[hidden email]> wrote:
>
> Both of the following code bits seem to do the same thing...
>
>    if let foo = optionalFoo,
>       let bar = optionalBar {
>        // ...
>    }
>
>    if let foo = optionalFoo,
>           bar = optionalBar {
>        // ...
>    }
>
> Is there any reason why one should be preferred over the other?

You *must* use the former if you want to use a boolean condition between them with a "where" clause.  In the absence of a where clause, this is a style question.

IMHO, I'd use the former when wrapping the lines as you have, but use the later when both foo/bar are on the same line, e.g.:

  if let foo = optionalFoo, bar = optionalBar {
       // ...
   }

-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/BA41571C-0EC3-4287-BB25-4950520A3438%40apple.com.
For more options, visit https://groups.google.com/d/optout.


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

--
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/40EDC5E3-60A3-4250-91CE-4B6633EC4295%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: if let ...

Marco S Hyman
In reply to this post by Bruno Berisso
let op1: Any? = "test"
let op2: Any? = 1

if let a = op1 as? String where a == "test",
   let b = op2 as? Int where b > 1 {
    "work"
} else {
    "don't work”
}

That will work because all conditions are true.  Change op1 from “test” to
something else and “don’t work” is the result.  Change opt2 from 4 to 1 and
“don’t work” is the result, etc.

Tested with Xcode 7 beta 4

--
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/C830FE3A-DCFE-4316-91FA-C4BFA90D0755%40snafu.org.
For more options, visit https://groups.google.com/d/optout.