Last active
August 29, 2015 14:04
-
-
Save mbrandonw/5318952dc53ac7be0b95 to your computer and use it in GitHub Desktop.
Overloading Swift functions with only 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
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