Skip to content

Commit a955d8d

Browse files
author
Stephane Magne
committed
Merge branch 'stephane/optional_cases'
2 parents 0c925f2 + aa5e132 commit a955d8d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Sources/Meta/Switch.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
// Created by Théophane Rupin on 3/8/19.
66
//
77

8+
public enum SwitchCaseVariableOptionality {
9+
case some
10+
case none
11+
case notOptional
12+
}
13+
814
public struct SwitchCaseVariable: Hashable, MetaSwiftConvertible {
915

16+
public let optionality: SwitchCaseVariableOptionality
17+
1018
public let name: String
1119

1220
public var type: TypeIdentifier?
13-
14-
public init(name: String, as type: TypeIdentifier? = nil) {
21+
22+
public init(optionality: SwitchCaseVariableOptionality = .notOptional, name: String, as type: TypeIdentifier? = nil) {
23+
self.optionality = optionality
1524
self.name = name
1625
self.type = type
1726
}
@@ -113,7 +122,15 @@ extension Switch: FunctionBodyMember {}
113122
extension SwitchCaseVariable {
114123

115124
public var swiftString: String {
116-
return "let \(name)\(type?.swiftString.prefixed(" as ") ?? .empty)"
125+
let assignment = "let \(name)\(type?.swiftString.prefixed(" as ") ?? .empty)"
126+
switch optionality {
127+
case .some:
128+
return ".some(\(assignment))"
129+
case .none:
130+
return ".none"
131+
case .notOptional:
132+
return assignment
133+
}
117134
}
118135
}
119136

0 commit comments

Comments
 (0)