Last active
August 29, 2015 14:17
-
-
Save JadenGeller/ab5deac7abd16e169a75 to your computer and use it in GitHub Desktop.
Swift Overloading on a Function's Return Type
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
// Note these divide functions both except identical inputs | |
func divide(a: Int, b: Int) -> Int { | |
return a / b | |
} | |
func divide(a: Int, b: Int) -> Float { | |
return Float(a) / Float(b) | |
} | |
// and Swift figures out which one to use magically! | |
let x: Int = divide(3, 2) | |
let y: Float = divide(3, 2) | |
println(x) // -> 1 | |
println(y) // -> 1.5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment