extension String {
public func capitalized() -> String {
return "ModuleA"
}
}
extension String {
public func capitalized() -> String {
return "ModuleB"
}
}
import ModuleA
import ModuleB
let capitalized = "hello swift".capitalized()
print(capitalized)
error: ambiguous use of 'capitalized()'
let capitalized = "hello swift".capitalized()
^
ModuleA.String:2:17: note: found this candidate
public func capitalized() -> String
^
ModuleB.String:2:17: note: found this candidate
public func capitalized() -> String
^
import ModuleA precedence 100 // to avoid a new keyword we can use the existing "precedence" keyword, but precendece is not the best word to describe what's happening here.
import ModuleB // default precedence is 0.
let capitalizedA = "hello swift".capitalized() // will default to the higher precedence implementation.
print(capitalizedA) // prints "ModuleA".
let capitalizedB = "hello swift".capitalized() from ModuleB // if we want to override the default we can use "from" keyword to disambiguate.
print(capitalizedB) // prints "ModuleB".
import ModuleA priority 100 // better, because its priority is explicit.
import ModuleB // default priority is 0.
let capitalizedA = "hello swift".capitalized() // will default to the higher priority implementation.
print(capitalizedA) // prints "ModuleA".
let capitalizedB = "hello swift".capitalized() from ModuleB // if we want to override the default we can use "from" keyword to disambiguate.
print(capitalizedB) // prints "ModuleB".
import ModuleA // there's no default implementation or priority concept.
import ModuleB // all usages of ambigous properties/methods have to be disambiguated on call site.
let capitalizedA = "hello swift".capitalized() from ModuleA // use "from" keyword to disambiguate.
print(capitalizedA) // prints "ModuleA".
let capitalizedB = "hello swift".capitalized() from ModuleB // use "from" keyword to disambiguate.
print(capitalizedB) // prints "ModuleB".
import ModuleA // there's no default implementation or priority concept.
import ModuleB // all usages of ambigous properties/methods have to be disambiguated on call site.
let capitalizedA = "hello swift".capitalized()@ModuleA // use @ symbol to disambiguate.
print(capitalizedA) // prints "ModuleA".
let capitalizedB = "hello swift".capitalized()@ModuleB // use @ symbol to disambiguate.
print(capitalizedB) // prints "ModuleB".
import ModuleA
import ModuleB
// the application of "from" keyword in chaining would be the same as the "as" keyword.
let stringB = ("hello swift".capitalized() from ModuleB).lowercased()
print(stringB) // prints "moduleb".
let stringB = "hello swift".capitalized()@ModuleB.lowercased()
print(stringB) // prints "moduleb".