Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active August 29, 2015 14:04
Show Gist options
  • Save mbrandonw/5318952dc53ac7be0b95 to your computer and use it in GitHub Desktop.
Save mbrandonw/5318952dc53ac7be0b95 to your computer and use it in GitHub Desktop.
Overloading Swift functions with only return type.
func f (x: Int) -> Int {
return 1 + x + x*x
}
func f (x: Int) -> String {
return "result = \(f(x) as Int)"
}
// awesome!
var x: Int = f(2) // 7
var y: String = f(2) // "result = 7"
// sweet!
var z = f(2) // Error: Ambiguous use of 'f'
// is type inferred by composition?
func square (x: Int) -> Int {
return x * x
}
square(f(2)) // 49 ... it is!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment