What is this Self Expression: self(initializer arguments)?

The Swift Programming Language in Self Expression:

I thought it's just the same as self.init(...), but seems not:

struct Foo {
    let n: Int
}

extension Foo {
    init(blah: Bool) {
        self(n: blah ? 1 : 0)  // Cannot call value of non-function type 'Foo'
    }
}

Should be for when self is callable (SE-0253: Callable values of user-defined nominal types), e.g.,

struct Foo {
    func callAsFunction(arg0: Int) {
        print("Called with arg0: \(arg0)")
    }
    func bar() {
        self(arg0: 7)
    }
}

Foo().bar() // Called with arg0: 7
2 Likes

Maybe it shouldn't use initializer arguments, should be callAsFunction parameters?

Terms of Service

Privacy Policy

Cookie Policy