-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExistentialBox.swift
91 lines (89 loc) · 2.51 KB
/
ExistentialBox.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// Container opening existentials
///
/// By leveraging power of conditional conformances this type serves as a castable container for `_openExistential` function
///
/// Examples:
///
/// ```swift
/// import SwiftUI
///
/// // Declare required type-erased properties or methods in a separate private protocol
///
/// private protocol AnyViewConforming {
/// var body: AnyView { get }
/// }
///
/// // Conform `ExistentialBox` to given protocol when Content conforms/inherits base protocol/type
/// extension ExistentialBox: AnyViewConforming where Content: View {
/// fileprivate var body: AnyView { AnyView(content) }
/// }
///
/// // Use `open` function to access/process data
/// extension AnyView {
/// init?(any: Any) {
/// guard let content = open(any, \AnyViewConforming.content) else { return nil }
/// self = content
/// }
/// }
/// ```
///
/// Similarly you can process mutually-dependant generic types
///
/// ```swift
/// // MARK: - Declaration
///
/// public protocol InitializableWithNoArguments {
/// init()
/// }
///
/// public protocol UIComponent<Model>: InitializableWithNoArguments {
/// associatedtype Model: UIComponentModel<Self>
/// func setModel(_ model: Model)
/// }
///
/// public protocol UIComponentModel<Component> {
/// associatedtype Component: UIComponent<Self>
/// func createComponent() -> Component
/// }
///
/// extension UIComponentModel {
/// public func createComponent() -> Component {
/// let component = Component()
/// component.setModel(self)
/// return component
/// }
/// }
///
/// // MARK: - Opening existentials (probably a separate file)
///
/// private protocol UIComponentConforming {
/// func setAnyModel(_ model: any UIComponentModel)
/// }
///
/// extension ExistentialBox: UIComponentConforming where Content: UIComponent {
/// fileprivate func setAnyModel(_ model: any UIComponentModel) {
/// (model as? Content.Model).map(content.setModel)
/// }
/// }
///
/// extension ExistentialBox<(any UIComponent)> {
/// public func setModel(_ model: any UIComponentModel) {
/// ExistentialBox<UIComponentConforming>.open(content) { $0.setAnyModel(model) }
/// }
/// }
/// ```
public struct ExistentialBox<Content> {
public let content: Content
public init(_ content: Content) {
self.content = content
}
}
extension ExistentialBox {
public static func open<Output>(
_ value: Any,
as targetType: Content.Type = Content.self,
with action: (Content) -> Output
) -> Output? {
ExistentialContainer.open(value, as: targetType, with: action)
}
}