Swift: Trailing closures do not work with default parameters
| Originator: | zachthayer | ||
| Number: | rdar://17156774 | Date Originated: | 04-Jun-2014 |
| Status: | Duplicate | Resolved: | 16535452 |
| Product: | Swift Compiler | Product Version: | Swift version 1.0 (swift-600.0.34.4.5) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
When attempting to create a function that receives both a default parameters and a closure, it becomes a impossible to use a trailing closure when specifying an explicit value for one of the default parameters.
Steps to Reproduce:
1. Open the provided Playground
2. Try to utilize the various foo functions and invocations provided
Expected Results:
It is expected that a function with default parameters should also be able to use trailing closures and still maintain the ability to specify values when needed. Such as
define function as (a = 1, closure)
function(){} // Default value is used
function(a: 10){} // Custom value is used
Actual Results:
The compiler was unable to interpret the desired behavior, resulting in trailing closures unable to be utilized in conjunction with default parameters
Version:
Xcode 6.0 (6A215l), Swift version 1.0 (swift-600.0.34.4.5), OSX 10.9.3 (12D65)
Notes:
Configuration:
Attachments:
'Playground.playground.zip' was successfully uploaded.
// Playground - noun: a place where people can play
import Cocoa
func foo(done: ((String) -> Void), bar: String = "default"){
done(bar)
}
// It works!, but what if I want to set bar?
foo(){(bar: String) in
println(bar)
}
// No workie
//foo(bar: "test"){(bar: String) in
// println(bar)
//}
// Ugh I guess rewrite, but works
foo({(bar: String) in
println(bar)
}, bar: "test")
func foo2(bar: String = "z", done: ((String) -> Void)){
done(bar)
}
// No workie
//foo2(){(bar: String) in
// println(bar)
//}
// Works now, but still not usage of defaults
foo2(bar: "test2"){(bar: String) in
println(bar)
}
func foo3(bar: String = "z", _ done: ((String) -> Void) = {x in println("default")}
){
done(bar)
}
// No workie
//foo3(){(bar: String) in
// println(bar)
//}
// Works, but no default value
foo3(bar: "test3"){(bar: String) in
println(bar)
}
// Works, but useless
foo3()
foo3(bar: "test4")
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!