Swift: Mistakenly passing a function instead of a function call in to a generic function can cause silent failure.
| Originator: | amber | ||
| Number: | rdar://20887111 | Date Originated: | 09-May-2015 02:23 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | 1.2 |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
Mistakenly passing in a function to a generic function, because you left out the parentheses after the function name can silently fail, and cause bugs.
If one has a generic function that takes element V :
func encode<V>(element: V?, forKey key: String) {
if let e = element {
do something with element
}
}
At the call site the programmer does something like this:
encoder.encode(Item.version, forKey:Item.versionKey)
Item.Version is a function, so this was a mistake and they really meant to call the function:
encoder.encode(Item.version(), forKey:Item.versionKey)
This causes a silent failure in this case.
Notes: In this example, version should probably be a calculated property. This can probably be circumvented with conventions, and practices, however I have run in to this myself a few times, and it is kind of scary knowing this potential error can be made. I just wanted to run it by you guys to let you know.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!