TIL: A pleasant surprise when passing closures as parameters

This one might be obvious, but it was a very pleasant surprise.

Let’s say we have something like this:

struct Receiver {
    func doAThing(_ closure:() -> Void) {
        closure()
    }
}

And a regular function like:

func theClosure() {
    print("Body of the closure")
}

Then, Xcode will provide this code hint:

And now I can do:

let aReceiver = Receiver()
aReceiver.doAThing {
    print("an specific thing that is not what theClosure does")
}

aReceiver.doAThing(theClosure)

Which I guess is not that unexpected. But still, a pleasant surprise.

Leave a Reply

Your email address will not be published. Required fields are marked *