Hello Swift users,

I have a question about capturing references to initializers. You can do
the following right now:

struct Foo {
    let number : Int
    // This type has a single initializer
    init() {
        number = 10
    }
}

let a  = Foo.init  // a's type: () -> Foo

So far so good. Now, let's add in a second initializer:

extension Foo {
    init(customNumber: Int) {
        number = customNumber
    }
}

Now, if you want to capture a reference to one of the initializers, you can
annotate the variable with the explicit function type:

let a : () -> Foo = Foo.init
let b : Int -> Foo = Foo.init

My question involves the case where you have multiple initializers that
take the same arguments and types. How would you capture initializers then?

extension Foo {
    init(numberToInc: Int) {
        number = numberToInc + 1
    }
}

How do I capture a reference to the numberToInc: initializer, versus the
customNumber: initializer?

Best regards,
Austin
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to