From 5d76f503a42d6c33a696c845ae14575ebf259008 Mon Sep 17 00:00:00 2001 From: ktiays Date: Sun, 15 Oct 2023 01:41:01 +0800 Subject: [PATCH] Optimize `Defaults` property wrapper --- Sources/CyanUtils/Defaults.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/CyanUtils/Defaults.swift b/Sources/CyanUtils/Defaults.swift index c2f7c48..754b509 100644 --- a/Sources/CyanUtils/Defaults.swift +++ b/Sources/CyanUtils/Defaults.swift @@ -69,11 +69,18 @@ extension Data: ConstructibleFromDefaults { public struct Defaults where T: ConstructibleFromDefaults { public let key: String - public let defaultValue: T + public var defaultValue: T { + `default`() + } + private let `default`: () -> T - public init(key: String, defaultValue: T) { + public init(key: String, default: @escaping () -> T) { self.key = key - self.defaultValue = defaultValue + self.default = `default` + } + + public init(key: String, defaultValue: T) { + self.init(key: key) { defaultValue } } public var wrappedValue: T {