Swift 5.7 - implementing Function Overloading in Generic Extensions for Collection Types
I'm stuck on something that should probably be simple. I'm relatively new to this, so bear with me. I've looked through the documentation and I'm still confused about I'm experiencing a strange scenario with function overloading in a generic extension for Swift collection types. I have defined two methods in an extension of `Array` that should handle different types of elements, but the compiler seems to be getting confused and throwing the following behavior: ``` Ambiguous use of 'methodName' ``` Here's what my code looks like: ```swift extension Array { func methodName<T: Numeric>(using value: T) -> [T] { return self.compactMap { ($0 as? T) } } func methodName(using value: String) -> [String] { return self.compactMap { ($0 as? String) } } } ``` I've tried calling these methods with different types of arrays to see what would happen: ```swift let intArray: [Any] = [1, 2, 3] let result1 = intArray.methodName(using: 2) // Should return [2] let stringArray: [Any] = ["hello", "world"] let result2 = stringArray.methodName(using: "world") // Should return ["world"] ``` However, both calls throw the ambiguous use behavior. I suspect it might be due to the generic constraints in the first method, but I'm not sure how to resolve this. I've also tried changing type constraints and using different parameter names, but nothing seems to work. Is there a way to make the compiler differentiate between these overloaded methods in this context? Any suggestions would be greatly appreciated! This is part of a larger API I'm building. Any ideas what could be causing this? For context: I'm using Swift on Ubuntu. How would you solve this? How would you solve this?