Created
June 21, 2017 17:27
-
-
Save jeremiegirault/9123aeba3bf95d376291186a92397fbc to your computer and use it in GitHub Desktop.
Interesting way to use swift autoclosure for dynamic values
This file contains hidden or 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
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