Skip to content

Instantly share code, notes, and snippets.

@jeremiegirault
Created June 21, 2017 17:27
Show Gist options
  • Save jeremiegirault/9123aeba3bf95d376291186a92397fbc to your computer and use it in GitHub Desktop.
Save jeremiegirault/9123aeba3bf95d376291186a92397fbc to your computer and use it in GitHub Desktop.
Interesting way to use swift autoclosure for dynamic values
struct Dynamic<T> {
let get: () -> T
init(_ get: @autoclosure @escaping () -> T) {
self.get = get
}
var value: T { return get() }
}
var i: Int = 0
let dynamic = Dynamic(i)
dynamic.value // 0
i = 1
dynamic.value // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment