How does the Swift compiler choose between two Int initializers with a String parameter?

66 views Asked by At

I recently found an Int initializer:

init?<S>(_ text: S, radix: Int = 10) where S: StringProtocol

As you can see, the second parameter radix has a default value with 10, so I can call this function without providing it.

Meanwhile, String does conform to this StringProtocol through the following extension:

extension String : StringProtocol { 
    //... 
}

And there is another initializer for Int:

init?(_ description: String)

Given the code:

let x = Int("123")

How could the Swift compiler know which initializer to call as this statement matches both of them. What factors influence its decision-making process?

0

There are 0 answers