Passing functions with default parameters


(Oskar Ek) #1

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?


(Ben Cohen) #2

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.

Thanks,
Ben

···

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