Last active
March 21, 2016 18:26
-
-
Save tjw/4904ac82f84adf9f2292 to your computer and use it in GitHub Desktop.
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(code:Void -> Void) { | |
print("no throw version called") | |
code() | |
} | |
func f<Result>(code:Void throws -> Result) rethrows -> Result { | |
print("rethrow version called") | |
return try code() | |
} | |
struct Error : ErrorType {} | |
func voidReturn1() { | |
print("voidReturn1") | |
} | |
func voidReturn2() { | |
print("voidReturn2") | |
} | |
func intReturn(arg:Int) -> Int { | |
print("intReturn") | |
return arg | |
} | |
func voidWithThrow() throws { | |
print("voidWithThrow") | |
throw Error() | |
} | |
func intReturnThrows(arg:Int) throws -> Int { | |
print("intReturnThrows") | |
throw Error() | |
} | |
print("Void return (1 stmt):") | |
f { | |
voidReturn1() | |
} | |
print("") | |
print("Void return (2 stmts):") | |
f { | |
voidReturn1() | |
voidReturn2() | |
} | |
print("") | |
print("Int return and not using the result:") | |
f { | |
intReturn(0) | |
} | |
print("") | |
print("Int return and using the result:") | |
let x = f { | |
intReturn(1) | |
} | |
print("returned \(x)") | |
print("") | |
print("Void throw:") | |
do { | |
try f { | |
try voidWithThrow() | |
} | |
} catch let e { | |
print("caught \(e)") | |
} | |
print("") | |
print("Int throw:") | |
do { | |
try f { | |
try intReturnThrows(2) | |
} | |
} catch let e { | |
print("caught \(e)") | |
} | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment