[swift-evolution] When exactly 1 function matches a call site, should it be called or cause a compiler error?

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Sun Nov 12 19:12:31 CST 2017


I brought this up on Swift Dev and was told to check on Swift Evolution to
see if a proposal is needed.

Currently, the following code produces a compiler error:

func foo(_ x: Int, _ y: Int) -> Int {
  return x + y
}

extension Int {
  func foo() -> Int {
    return foo(self, self)    // Error here
  }
}

Notice that the two functions named “foo” have entirely different
signatures. The global function takes 2 arguments, while the member
function takes 0 (or 1, if referenced as “Int.foo”).

There is exactly one function “foo” which takes 2 arguments, so a call to
“foo” with 2 arguments, like the one shown, should be unambiguous. However,
instead of calling the function with matching signature, there is instead a
compiler error.

This is already documented as SR–2450
<https://bugs.swift.org/browse/SR-2450>, with an example from the standard
library (global 2-argument “min” vs. 0-argument “Collection.min”).

It appears that in any situation where a global function and a member
function share the same base name, but only the global function’s signature
matches the call site, the result is a compiler error.

I suggest that, when there is exactly one function available with the
proper name and signature, instead of a compiler error the
matching function should be called.

Do we need a Swift Evolution proposal for this change?

Nevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171112/a57a44d0/attachment.html>


More information about the swift-evolution mailing list