Last active
August 25, 2016 20:43
-
-
Save patr1ck/c6f425d5f627a516d708 to your computer and use it in GitHub Desktop.
Polymorphic Function Returns in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In Swift, functions can be defined with identical parameter types, but different return types. | |
func isHello(string: String) -> Bool { | |
if string == "Hello" { | |
return true | |
} else { | |
return false | |
} | |
} | |
func isHello(string: String) -> String { | |
if string == "Hello" { | |
return "Yep" | |
} else { | |
return "Nope" | |
} | |
} | |
let numberResult: Bool = isHello("Hello") | |
// => true | |
let stringResult: String = isHello("Hello") | |
// => "Yep" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment