[swift-evolution] Passing functions with default parameters

Ben Cohen ben_cohen at apple.com
Sun Jun 18 21:41:59 CDT 2017

Hi Oskar,

This is actually something we’ve spoken about in the core team meetings in the past as a known thing that ideally would work but doesn’t. The one that hits me a often is that I wish .forEach(print) worked, as it’s very useful as a quick debugging command.

I don’t know if we’re tracking it on bugs.swift.org though, so it’d be good to see if you can find an SR for it there and if not, put your very clear explanation of the issue in a new SR.


> On Jun 17, 2017, at 5:14 PM, Oskar Ek via swift-evolution <swift-evolution at swift.org> wrote:
> 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?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170618/5809129a/attachment.html>

More information about the swift-evolution mailing list