Skip to content

Instantly share code, notes, and snippets.

@tjw
Last active March 21, 2016 18:26
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 tjw/4904ac82f84adf9f2292 to your computer and use it in GitHub Desktop.
Save tjw/4904ac82f84adf9f2292 to your computer and use it in GitHub Desktop.
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