Created
June 11, 2014 12:09
-
-
Save Ciechan/389d4174c3d41828d642 to your computer and use it in GitHub Desktop.
Hacky private fields
This file contains 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
class Counter { | |
let inc : () -> Int | |
let dec : () -> Int | |
init(inc : () -> Int, dec : () -> Int) { | |
self.inc = inc; | |
self.dec = dec; | |
} | |
} | |
func CounterCreator(initial :Int) -> Counter { | |
var storage = initial | |
return Counter(inc: {return ++storage}, dec: {return --storage}) | |
} | |
let c = CounterCreator(10) | |
c.inc() // 11 | |
c.inc() // 12 | |
c.dec() // 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the explicit return for one line expressions, you can remove the returns from the closures
return Counter(inc: {++storage}, dec: {--storage})