[swift-evolution] Closures from methods with default args

James Froggatt james.froggatt at me.com
Thu Jan 19 20:16:42 CST 2017


I'd also like to see a solution to this, FWIW.
Sometime's it's hard to tell whether something is its own function, or another function with with some defaulted parameters, so I could imagine this being a source of confusion.

> I came across something that I'm not sure it's a bug or by design and if it's by design, whether this should be discussed here.
> 
> Example:
> 
> class Foo {
> init(number: Int) { /* ... */ }
> }
> 
> let closure = Foo.init(number:) // (Int) ->Foo
> [1, 2, 3].map(closure) // [Foo, Foo, Foo]
> 
> This works great until the initializer gets a default argument:
> 
> class Foo {
> init(number: Int, string: String = "") { /* ... */ }
> }
> 
> // Error: Foo has no member init(number:)
> let closure = Foo.init(number:)
> 
> I was wondering if we could get closures to methods without the default arguments. Currently, this needs to be worked around by e.g. creating a second closure that invokes the method without the default arguments:
> 
> let closure: (Int) ->Foo = { Foo(number: $0) }
> 
> But to me it seems like something that should work "out of the box".
> 
> Thoughts?
> 
> 
> 


More information about the swift-evolution mailing list