Ambiguous use of "a same name var or func" of extension from different modules

Same issue with this topic: Name disambiguation of computed property/function with same type defined in extensions

MuduleA

extension String {
    public func capitalized() -> String {
        return "ModuleA"
    }
}

ModuleB

extension String {
    public func capitalized() -> String {
        return "ModuleB"
    }
}

ModuleC

import ModuleA
import ModuleB

let capitalized = "hello swift".capitalized()
print(capitalized)

Error: Ambiguous use of 'capitalized'

After reading the blog: Swift Import Declarations - NSHipster, we can import individual declarations, such as:

// ModuleA
func foo() {}

// ModuleC
import func ModuleA.foo

foo()

But swift do not support import extensions like this. So do we need a proposal like this:

// ModuleA
extension String {
    public func capitalized() -> String {
        return "ModuleA"
    }
}

// MuduleC
import extension ModuleA.String.capitalized
or 
import String.extension ModuleA.capitalized
or 
import extension.String ModuleA.capitalized
1 Like

Correct, because extensions are not considered to be entities that "exist" in the same way like types and their members do.

You are correct that there is no current way to disambiguate between implementations vended by different modules extending the same type—this is a topic that has come up repeatedly, including just yesterday in a discussion about conditionals to test symbol availability (you can use the search function on this forum to find other such discussions).

Once that is possible, it becomes unnecessary to import specific extensions, as you can just refer to the extension in question at the point of use (for example, in one syntax that's been pitched before, you might write "hello swift".ModuleA::capitalized() to disambiguate).

One possible workaround for now is to create wrapper functions that essentially rename the two conflicting extensions, but that is clunky.

3 Likes