nope. it's the same as this example: func funny() -> Int { return 1 }
func funny() -> String { return "2" } print(funny()) // the compiler doesn't know which one you want. // the above doesn't compile. // error: forloop.playground:8:1: error: ambiguous use of 'funny()' You have to have some difference visible in the caller: func funny(useInt: Bool) -> Int { return 1 } func funny(useString: Bool) -> String { return "2" } print(funny(useInt: true), funny(useString: true)) // prints "1 2\n" -- C. Keith Ray Senior Software Engineer / Trainer / Agile Coach * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf * https://leanpub.com/wepntk <https://leanpub.com/wepntk> <- buy my book? > On Oct 10, 2017, at 9:06 AM, Phil Kirby via swift-users > <swift-users@swift.org> wrote: > > <>2 down vote <> favorite > <https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type#> > > Original StackOverflow post: > > https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type > > <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
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users