Initializable.swift 705 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Initializable.swift
  3. // SideMenu
  4. //
  5. // Created by Jon Kent on 7/2/19.
  6. //
  7. import Foundation
  8. internal protocol InitializableClass: class {
  9. init()
  10. }
  11. extension InitializableClass {
  12. init(_ block: (Self) -> Void) {
  13. self.init()
  14. block(self)
  15. }
  16. @discardableResult func with(_ block: (Self) -> Void) -> Self {
  17. block(self)
  18. return self
  19. }
  20. }
  21. public protocol InitializableStruct {
  22. init()
  23. }
  24. public extension InitializableStruct {
  25. init(_ block: (inout Self) -> Void) {
  26. self.init()
  27. block(&self)
  28. }
  29. @discardableResult mutating func with(_ block: (inout Self) -> Void) -> Self {
  30. block(&self)
  31. return self
  32. }
  33. }