Skip to content

Add documentation comments and a defaulting subscript to `Storage`.

Compare
Choose a tag to compare
@VaporBot VaporBot released this 21 Jul 06:41
· 162 commits to main since this release
d9d83cd
This patch was authored and released by @gwynne.

The new subscript simplifies "provider" implementations that extend Application and use its Storage instance without complex initialization requirements:

extension Application {
    public struct Foo {
        final class Storage { /* content which needs no special initialization */ }
        struct Key: StorageKey { typealias Value = Storage }
        let application: Application

        // Before:
        var storage: Storage {
            if self.application.storage[Key.self] == nil { self.initialize() }
            return self.application.storage[Key.self]!
        }

        func initialize() { self.application.storage[Key.self] = .init() }

        // After:
        var storage: Storage { self.application.storage[Key.self, default: .init()] }