From 632f7498bcbf6182b6960345896391eec25d6dbb Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 22 Aug 2022 17:43:28 -0500 Subject: [PATCH] created static initWith for withable Signed-off-by: Matt Bruce --- VDS/Protocols/Initable.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/VDS/Protocols/Initable.swift b/VDS/Protocols/Initable.swift index 8a6eacb9..05cb98d6 100644 --- a/VDS/Protocols/Initable.swift +++ b/VDS/Protocols/Initable.swift @@ -10,3 +10,14 @@ import Foundation public protocol Initable { init() } + +extension Withable where Self:Initable { + /// Provides a closure to configure instances inline. + /// - Parameter closure: A closure with a mutable copy of `self` as the argument. + /// - Returns: Simply returns the mutated copy of the instance after called the `closure`. + @discardableResult public static func initWith(_ closure: (_ instance: inout Self) -> Void) -> Self { + var copy = Self() + closure(©) + return copy + } +}