Skip to content

Commit

Permalink
Support Swift 5.9 repeat/each in ViewBuilder and TupleView
Browse files Browse the repository at this point in the history
  • Loading branch information
treastrain committed Sep 16, 2023
1 parent e0d8e9d commit ba677b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/TokamakCore/Views/Containers/TupleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public struct TupleView<T>: _PrimitiveView {
let _children: [AnyView]
private let visit: (ViewVisitor) -> ()

@_disfavoredOverload
public init(_ value: T) {
self.value = value
_children = []
Expand All @@ -44,6 +45,16 @@ public struct TupleView<T>: _PrimitiveView {
visit(visitor)
}

#if swift(>=5.9)
init<each Content: View>(_ v: (repeat each Content)) where T == (repeat each Content) {
var children: [AnyView] = []
func append<V: View>(_ view: V) {
children.append(.init(view))
}
repeat append(each v)
self.init(v, children: children)
}
#else
init<T1: View, T2: View>(_ v1: T1, _ v2: T2) where T == (T1, T2) {
value = (v1, v2)
_children = [AnyView(v1), AnyView(v2)]
Expand Down Expand Up @@ -263,6 +274,7 @@ public struct TupleView<T>: _PrimitiveView {
$0.visit(v10)
}
}
#endif
}

extension TupleView: GroupView {
Expand Down
8 changes: 8 additions & 0 deletions Sources/TokamakCore/Views/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public enum ViewBuilder {
// swiftlint:disable large_tuple
// swiftlint:disable function_parameter_count

#if swift(>=5.9)
public extension ViewBuilder {
static func buildBlock<each Content: View>(_ content: repeat each Content) -> TupleView<(repeat each Content)> {
TupleView((repeat each content))
}
}
#else
public extension ViewBuilder {
static func buildBlock<C0, C1>(_ c0: C0, _ c1: C1) -> TupleView<(C0, C1)>
where C0: View, C1: View
Expand Down Expand Up @@ -243,3 +250,4 @@ public extension ViewBuilder {
TupleView(c0, c1, c2, c3, c4, c5, c6, c7, c8, c9)
}
}
#endif

0 comments on commit ba677b2

Please sign in to comment.