Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active August 29, 2015 14:05
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 bjhomer/b5187a119fe6334989fb to your computer and use it in GitHub Desktop.
Save bjhomer/b5187a119fe6334989fb to your computer and use it in GitHub Desktop.
This does not compile.
class Foo {
var closure: () -> () = {}
init() {}
func nothing() {}
func setupClosure() {
closure = {
[weak self] in
self?.nothing() // <-- this returns a "Void?", which means that the rhs's
// inferred type does not match the lhs's type.
}
}
func setupClosure2() {
closure = {
[weak self] () -> () in
self?.nothing() // <-- Also fails to compile, because inferred type does not
// match explicit type declaration
}
}
func setupClosure3() {
closure = {
[weak self] in
self?.nothing(); return // <-- This works, because it's no longer a single-expression
// closure, and thus it's no longer inferring a "Void?"
// return type.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment