@@ -34,56 +34,6 @@ public struct Guard: CodeBlock, Sendable {
3434 private let conditions : [ any CodeBlock ]
3535 private let elseBody : [ any CodeBlock ]
3636
37- /// Creates a `guard` statement.
38- /// - Parameters:
39- /// - condition: A ``CodeBlockBuilder`` that provides the condition expression.
40- /// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
41- public init (
42- @CodeBlockBuilderResult _ condition: ( ) throws -> [ any CodeBlock ] ,
43- @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
44- ) rethrows {
45- let allConditions = try condition ( )
46- if allConditions. isEmpty {
47- // Use true as default condition when no conditions are provided
48- self . conditions = [ Literal . boolean ( true ) ]
49- } else {
50- self . conditions = allConditions
51- }
52- self . elseBody = try elseBody ( )
53- }
54-
55- /// Creates a `guard` statement without a condition (uses true as default).
56- /// - Parameters:
57- /// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
58- public init (
59- @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
60- ) rethrows {
61- try self . init (
62- [ any CodeBlock ] . init,
63- else: elseBody
64- )
65- }
66-
67- /// Creates a `guard` statement with a string condition.
68- /// - Parameters:
69- /// - condition: The condition as a string.
70- /// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
71- public init (
72- _ condition: String ,
73- @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
74- ) rethrows {
75- self . conditions = [ VariableExp ( condition) ]
76- self . elseBody = try elseBody ( )
77- }
78-
79- /// Convenience initializer that accepts a single condition ``CodeBlock``.
80- public init (
81- _ condition: any CodeBlock ,
82- @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
83- ) rethrows {
84- try self . init ( { condition } , else: elseBody)
85- }
86-
8737 public var syntax : any SyntaxProtocol {
8838 // MARK: Build conditions list (mirror implementation from `If`)
8939 let condList = ConditionElementListSyntax (
@@ -165,4 +115,54 @@ public struct Guard: CodeBlock, Sendable {
165115 )
166116 )
167117 }
118+
119+ /// Creates a `guard` statement.
120+ /// - Parameters:
121+ /// - condition: A ``CodeBlockBuilder`` that provides the condition expression.
122+ /// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
123+ public init (
124+ @CodeBlockBuilderResult _ condition: ( ) throws -> [ any CodeBlock ] ,
125+ @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
126+ ) rethrows {
127+ let allConditions = try condition ( )
128+ if allConditions. isEmpty {
129+ // Use true as default condition when no conditions are provided
130+ self . conditions = [ Literal . boolean ( true ) ]
131+ } else {
132+ self . conditions = allConditions
133+ }
134+ self . elseBody = try elseBody ( )
135+ }
136+
137+ /// Creates a `guard` statement without a condition (uses true as default).
138+ /// - Parameters:
139+ /// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
140+ public init (
141+ @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
142+ ) rethrows {
143+ try self . init (
144+ [ any CodeBlock ] . init,
145+ else: elseBody
146+ )
147+ }
148+
149+ /// Creates a `guard` statement with a string condition.
150+ /// - Parameters:
151+ /// - condition: The condition as a string.
152+ /// - elseBody: A ``CodeBlockBuilder`` that provides the body when the condition is false.
153+ public init (
154+ _ condition: String ,
155+ @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
156+ ) rethrows {
157+ self . conditions = [ VariableExp ( condition) ]
158+ self . elseBody = try elseBody ( )
159+ }
160+
161+ /// Convenience initializer that accepts a single condition ``CodeBlock``.
162+ public init (
163+ _ condition: any CodeBlock ,
164+ @CodeBlockBuilderResult else elseBody: ( ) throws -> [ any CodeBlock ]
165+ ) rethrows {
166+ try self . init ( { condition } , else: elseBody)
167+ }
168168}
0 commit comments