Method parameter shadowing

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

Method parameter shadowing

Boon Chew

Swift allows you to shadow method parameter without providing any warning. 
In what circumstances would such a feature be useful? Also, this seems to go against the premise of Swift to promote safety.

var x:Int? = 3

func foo(x: Int?) {
    print(x)
    let x = 100  // shadowing parameter
    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/e1bb6f31-b97b-485e-9a6c-e82ac2d62216%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Method parameter shadowing

Jim Dovey
I believe it’s by design, to ease working with Optionals. For instance, in v2 syntax, you can have the following to ‘promote’ an optional argument into a non-nil value:

func foo(x: Int?) -> Int? {
    guard let x = x else { return nil }
    return x + 10
}

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


Swift allows you to shadow method parameter without providing any warning. 
In what circumstances would such a feature be useful? Also, this seems to go against the premise of Swift to promote safety.

var x:Int? = 3

func foo(x: Int?) {
    print(x)
    let x = 100  // shadowing parameter
    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/e1bb6f31-b97b-485e-9a6c-e82ac2d62216%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/F76EAD9B-FDBE-4806-B723-2021A5D67DB5%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Method parameter shadowing

Boon Chew
In reply to this post by Boon Chew
That helps, thank you Jim.

On Wednesday, June 17, 2015 at 1:52:07 PM UTC-4, Boon wrote:

Swift allows you to shadow method parameter without providing any warning. 
In what circumstances would such a feature be useful? Also, this seems to go against the premise of Swift to promote safety.

var x:Int? = 3

func foo(x: Int?) {
    print(x)
    let x = 100  // shadowing parameter
    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/789f3b9e-8b5a-4c98-ad92-3eccc1b10f42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.