swiftxcodeprotocol-extension

Is this a bug in Xcode or a practice should be avoid by programmer?


In the first place, I looked for a way to simplify coding by providing default argument values for protocol functions. I took the solution here and then found some fatal subsequence it may bring:

protocol Foo {
    func foo(_ a: Int)
}

extension Foo {
    func foo(_ a: Int = 4) {
        foo(a)
    }
}

struct FooImpl: Foo {
    // empty implementation
}

FooImpl().foo() // will go recursively forever and finally reach the stack limit

I also found that this code would fail to compile in IBM Swift Sandbox thus supposing xcode compiler might be to blame.


Solution

  • This looks to be valid Swift code that the compiler should accept and execute. That it contains a fatal infinite recursion is a logic error on the part of the programmer.

    I don't see anything in the IBM Swift Sandbox to indicate that it handles the code any better, or differently, than Xcode.