Why Swift now compile the same variable and function name

After upgrade the Xcode to 12.5. I notice declare a variable with the same name method is no longer occurs compiler error.

Reference: Why doesn't Swift allow a variable and a function with the same name?

Is that a feature from some proposal? I'm not find anything about that from the release notes.

class ViewController: UIViewController {
    
    func hello() -> String {
        return "Hi"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let hello = hello()
        print(hello)
    }

}

Thanks.

1 Like

I don't think this is fully fixed yet. For example this still doesn't work (SR-14242):

func foo() -> Int {
    let bar = 10
    func bar(number: Int) -> Int { return number }
    return bar(number: bar)
    //     ^
    // error: cannot call value of non-function type 'Int'
}

Although, there is a difference between your and mine examples. In your example, the function and the variable are in different scopes, whereas in mine, they're in the same scope.

I tried to find information on why your example works by looking through search results of "shadow" and "overload" on bugs.swift.org, but found nothing.

Terms of Service

Privacy Policy

Cookie Policy