Computed Variables & Variable Observers

The Swift doc only mentions computed properties and property observers.

However, any variable can be computed, and any variable can have observers. For example, this code

func foo() {
    var a = 0 {
        willSet {
            print("willSet `a` from \(a) to \(newValue)")

        didSet {
            print("didSet `a` from \(oldValue) to \(a)")

    var b: Int {
        get {
            print("get `b`'s value from `a`: \(a)")
            return a

        set {
            print("set `b`'s value to `a`: \(newValue)")
            a = newValue

    b += 1


prints out

get `b`'s value from `a`: 0
set `b`'s value to `a`: 1
willSet `a` from 0 to 1
didSet `a` from 0 to 1

(You will see an additional get b's value from a: 1 in Playground because b's value is read by Playground for displaying.)