Why does guard let exhibit different scoping behavior?

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

Why does guard let exhibit different scoping behavior?

Boon Chew

Why does guard let x = x inside a method behave differently than outside?

Example code below is copied right out of Playground.

var x:Int? = 10000

func foo
(x: Int?) {
    guard let x
= x else {
       
// Value requirements not met, do something
       
return
   
}

   
//output: "10000\n"
   
print(x)
}

foo
(x)

guard let x
= x else {
   
throw NSError(domain: "d", code: 0, userInfo: nil)
}

//output: "Optional(10000)\n"
print(x)


--
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/e4ddb62f-68d6-4d03-b7d1-a04783550108%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why does guard let exhibit different scoping behavior?

Chris Lattner


On Jun 17, 2015, at 4:16 AM, Boon <[hidden email]> wrote:

Why does guard let x = x inside a method behave differently than outside?

This is a known bug in the compiler, it is mentioned in the release notes and will be fixed for a future beta.

-Chris


Example code below is copied right out of Playground.

var x:Int? = 10000

func foo
(x: Int?) {
    guard let x
= x else {
       
// Value requirements not met, do something
       
return
   
}

   
//output: "10000\n"
   
print(x)
}

foo
(x)

guard let x
= x else {
   
throw NSError(domain: "d", code: 0, userInfo: nil)
}

//output: "Optional(10000)\n"
print(x)


--
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/e4ddb62f-68d6-4d03-b7d1-a04783550108%40googlegroups.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/82A114E5-D664-42FB-9190-CD7F1D3C5CDD%40apple.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why does guard let exhibit different scoping behavior?

Boon Chew


ok great, thanks for confirmation.


On Wednesday, June 17, 2015 at 11:23:21 AM UTC-4, Chris Lattner wrote:


On Jun 17, 2015, at 4:16 AM, Boon <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="-LUcpLKWgPsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">bo...@...> wrote:

Why does guard let x = x inside a method behave differently than outside?

This is a known bug in the compiler, it is mentioned in the release notes and will be fixed for a future beta.

-Chris


Example code below is copied right out of Playground.

var x:Int? = 10000

func foo
(x: Int?) {
    guard let x
= x else {
       
// Value requirements not met, do something
       
return
   
}

   
//output: "10000\n"
   
print(x)
}

foo
(x)

guard let x
= x else {
   
throw NSError(domain: "d", code: 0, userInfo: nil)
}

//output: "Optional(10000)\n"
print(x)


--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="-LUcpLKWgPsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">swift-languag...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="-LUcpLKWgPsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">swift-l...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/swift-language/e4ddb62f-68d6-4d03-b7d1-a04783550108%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/msgid/swift-language/e4ddb62f-68d6-4d03-b7d1-a04783550108%40googlegroups.com?utm_medium\75email\46utm_source\75footer';return true;" onclick="this.href='https://groups.google.com/d/msgid/swift-language/e4ddb62f-68d6-4d03-b7d1-a04783550108%40googlegroups.com?utm_medium\75email\46utm_source\75footer';return true;">https://groups.google.com/d/msgid/swift-language/e4ddb62f-68d6-4d03-b7d1-a04783550108%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">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/f86b8dea-42ec-40de-9285-e9ffdbbb7525%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.