func funny() -> Int { return 1 } func funny() -> String { return "2" }
let i: Int = funny() let s: String = funny() Is fine. Since Swift knows the return type required. However, it can’t infer the type of: let q = funny() -- Howard. > On 11 Oct 2017, at 4:36 am, C. Keith Ray via swift-users > <swift-users@swift.org> wrote: > > You need to know that the NAME of a method or function isn't just outside the > parenthesis. > > The name of > > func IsTextEmpty(foo : String?) -> Bool? > > is > > "IsTextEmpty(foo:)" > > and the name of > > > func IsTextEmpty(text : String?) -> Bool > > is > > "IsTextEmpty(text:)" > > The argument labels are important. > > > func IsTextEmpty(foo : String?) -> Bool? { > return foo?.isEmpty > } > > func IsTextEmpty(text : String?) -> Bool { > guard let text = text else { > return true > } > > return text.isEmpty > } > > print(IsTextEmpty(foo: "1"), IsTextEmpty(text: "2")) > // prints Optional(false) false > > > -- > C. Keith Ray > Senior Software Engineer / Trainer / Agile Coach > * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf > > > >> On Oct 10, 2017, at 10:27 AM, C. Keith Ray <keith...@mac.com> wrote: >> >> 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 <- 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 >>> 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 >> > > _______________________________________________ > 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