2 down vote favorite Original StackOverflow post: https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type I was under the impression that swift can have overloaded methods that differ only in the type of object that the methods return. I would think that I could have two funcs with the same signature yet they differ in return type. import Foundation
// ambiguous use of 'IsTextEmpty(text:)' func IsTextEmpty(text : String?) -> Bool? { return text?.isEmpty } func IsTextEmpty(text : String?) -> Bool { guard let text = text else { return true } return text.isEmpty } let text: String? = nil if let empty = IsTextEmpty(text:"text") { print("Not Empty") } if IsTextEmpty(text: text) { print("Empty") } Here, both functions have the same input parameters but one func returns an optional Bool? and the other returns a Bool. In this case I get an error: ambiguous use of 'IsTextEmpty(text:)' If I change the name of one of the input parameters I no longer get the ambiguous error: // Works func IsTextEmpty(foo : String?) -> Bool? { return foo?.isEmpty } func IsTextEmpty(text : String?) -> Bool { guard let text = text else { return true } return text.isEmpty } let text: String? = nil if let empty = IsTextEmpty(foo:"text") { print("Not Empty") } if IsTextEmpty(text: text) { print("Empty") } Shouldn't the compiler detect that they are two distinct methods even though their return types are different, since an optional Bool? is a different type from a non-optional Bool?
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users