Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/ab5deac7abd16e169a75 to your computer and use it in GitHub Desktop.
Save JadenGeller/ab5deac7abd16e169a75 to your computer and use it in GitHub Desktop.
Swift Overloading on a Function's Return Type
// 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