[swift-evolution] Passing functions with default parameters
Oskar Ek
oskarek24 at gmail.com
Sat Jun 17 19:14:46 CDT 2017
Hello Swift evolution community!
Developing in Swift, I have many times found myself being frustrated that a function with default parameters can’t be passed to another function.
For example:
// sorts a string alphabetically
func sort(string: String, descending: Bool = false) -> String {
return String(string.sorted { descending ? $1 < $0 : $0 < $1 })
}
let strings = [”swift", ”apple”, ”ios”]
If I wanted to map over the strings and sort each one, I would expect this to work:
strings.map(sort(string:)) // expected output: [”fistw", ”aelpp”, ”ios”]
However, this produces the error: use of unresolved identifier 'sort(string:)’
Instead, I have to write:
strings.map { sort(string: $0) }
Anybody else that would appreciate this possibility?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170618/3853507a/attachment.html>
More information about the swift-evolution
mailing list