Skip to content

Instantly share code, notes, and snippets.

@narfdotpl
Forked from Ciechan/private
Created June 11, 2014 12:58
Show Gist options
  • Save narfdotpl/838db18ea43f71c63b23 to your computer and use it in GitHub Desktop.
Save narfdotpl/838db18ea43f71c63b23 to your computer and use it in GitHub Desktop.
class Counter {
let inc: () -> Int
let dec: () -> Int
init(start: Int) {
var n = start
inc = { ++n }
dec = { --n }
}
}
let c = Counter(start: 10)
c.inc() // 11
c.inc() // 12
c.dec() // 11
@Ciechan
Copy link

Ciechan commented Jun 22, 2014

By our discussion on friday, you can even do:

init(var start: Int) {
    inc = { ++start }
    dec = { --start }
}

@narfdotpl
Copy link
Author

@Ciechan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment