Articles, podcasts and news about Swift development, by John Sundell.

Calling initializers with dot syntax and passing them as closures

Published on 25 Mar 2017

Cool thing about Swift initializers: you can call them using dot syntax and pass them as closures! Perfect for mocking dates in tests.

class Logger {
    private let storage: LogStorage
    private let dateProvider: () -> Date
    init(storage: LogStorage = .init(), dateProvider: @escaping () -> Date = Date.init) { = storage
        self.dateProvider = dateProvider
    func log(event: Event) { event, date: dateProvider())

Support Swift by Sundell by checking out this sponsor:


Bitrise: Kick off 2022 by easily setting up fast, rock-solid continuous integration for your project with Bitrise. In just a few minutes, you can set up builds, tests, and automatic App Store and beta deployments for your project, all running in the cloud on every pull request and commit. Try it for free today.