Hmm… I was thinking maybe one of the shortcuts from SE-0380 might help clean up that code. I see some build failures trying to use an if expression (and switch expression) directly in require:
enum E {
case a(x: Int, y: Int)
case b
}
func f(e: E) {
let _: (x: Int, y: Int)? = if case .a(x: let x, y: let y) = e {
(x, y)
} else {
nil
}
let _: (x: Int, y: Int)? = switch e {
case .a(x: let x, y: let y):
(x, y)
default:
nil
}
}
@Test func test() throws {
let e = E.a(x: 1, y: 1)
let _ = try #require(
if case .a(x: let x, y: let y) = e {
(x, y)
} else {
nil
}
)
let _ = try #require(
switch e {
case .a(x: let x, y: let y):
(x, y)
default:
nil
}
)
}
Here are the errors:
macro expansion #require:1:22: error: 'if' may only be used as expression in return, throw, or as the source of an assignment
`- /Users/rick/Desktop/MyLibrary/Tests/MyLibraryTests/MyLibraryTests.swift:31:4: note: expanded code originates here
29 | nil
30 | }
31 | )
+--- macro expansion #require ---------------------------------------
|1 | Testing.__checkValue(if case .a(x: let x, y: let y) = e {
| | `- error: 'if' may only be used as expression in return, throw, or as the source of an assignment
|2 | (x, y)
|3 | } else {
+--------------------------------------------------------------------
32 |
33 | let _ = try #require(
/Users/rick/Desktop/MyLibrary/Tests/MyLibraryTests/MyLibraryTests.swift:26:5: error: 'if' may only be used as expression in return, throw, or as the source of an assignment
24 |
25 | let _ = try #require(
26 | if case .a(x: let x, y: let y) = e {
| `- error: 'if' may only be used as expression in return, throw, or as the source of an assignment
27 | (x, y)
28 | } else {
macro expansion #require:1:22: error: 'switch' may only be used as expression in return, throw, or as the source of an assignment
`- /Users/rick/Desktop/MyLibrary/Tests/MyLibraryTests/MyLibraryTests.swift:40:4: note: expanded code originates here
38 | nil
39 | }
40 | )
+--- macro expansion #require ---------------------------------------
|1 | Testing.__checkValue(switch e {
| | `- error: 'switch' may only be used as expression in return, throw, or as the source of an assignment
|2 | case .a(x: let x, y: let y):
|3 | (x, y)
+--------------------------------------------------------------------
41 | }
42 |
/Users/rick/Desktop/MyLibrary/Tests/MyLibraryTests/MyLibraryTests.swift:34:5: error: 'switch' may only be used as expression in return, throw, or as the source of an assignment
32 |
33 | let _ = try #require(
34 | switch e {
| `- error: 'switch' may only be used as expression in return, throw, or as the source of an assignment
35 | case .a(x: let x, y: let y):
36 | (x, y)
I'm not completely sure I understand why those expressions would fail to compile in require. I'm not blocked on this… but it would be a neat shortcut if it worked.