[swift-evolution] Closures from methods with default args

Charlie Monroe charlie at charliemonroe.net
Mon Jan 9 02:13:36 CST 2017


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170109/9f420955/attachment.html>


More information about the swift-evolution mailing list