Skip to content

Instantly share code, notes, and snippets.

@mfaani
Last active March 20, 2019 20:32
Show Gist options
  • Save mfaani/2a9d28492fba0bb431f387d88515375e to your computer and use it in GitHub Desktop.
Save mfaani/2a9d28492fba0bb431f387d88515375e to your computer and use it in GitHub Desktop.
how to write weird closures :)
class C {
lazy var x: () -> Void = {
return { print("hi") }
}()
lazy var y: (Int) -> Void = { // `input in` shouldn't be written here!
return { input in
print(input)
}
}()
var r: () -> Void = {
return { print("hi") }
}()
let t: () -> Void = {
return { print("bye") }
}()
let yy: (Int) -> Void = { // `input in` shouldn't be written here!
return { input in
print(input)
}
}()
}
let c = C()
let k = c.x
print(k) // (Function)
k() // hi
c.x() // hi
c.y(5) // hi
c.r()
c.r = c.t
print("new")
c.r
c.yy(10)
c.yy(15) // Can pass different arguemnt
c.t = c.r // Can't change the actual closure itself. It's a let. Needs to be changed to `var`
c.t()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment