Skip to content

Commit 20d3025

Browse files
authored
Adds a convenience for mapping Never outputs to other types (#340)
Adds a way to map a workflow's `Never` output type to a different type without generating compiler warnings. Fixes #339 I like this solution because the natural inclination is to simply delete the block containing unreachable code and that just works: ```diff - .mapOutput { switch $0 {} } + .mapOutput() ``` I'm happy to add a test using this but the true validation is to successfully build without warnings.
1 parent 3fa55ff commit 20d3025

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Workflow/Sources/AnyWorkflow.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,14 @@ extension AnyWorkflowConvertible {
195195
asAnyWorkflow().mapRendering(transform)
196196
}
197197
}
198+
199+
// MARK: -
200+
201+
extension AnyWorkflowConvertible where Output == Never {
202+
/// A convenience for workflows that don't produce output but want to change the declared output type.
203+
public func mapOutput<NewOutput>(to: NewOutput.Type) -> AnyWorkflow<Rendering, NewOutput> {
204+
mapOutput(mapNever(_:))
205+
}
206+
}
207+
208+
private func mapNever<T>(_ input: Never) -> T {}

Workflow/Tests/AnyWorkflowTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public class AnyWorkflowTests: XCTestCase {
8282

8383
XCTAssertNotNil(erased.base as? OnOutputWorkflow)
8484
}
85+
86+
func testMapNeverToOtherType() {
87+
// test that this `Output = Bool` workflow compiles when mapped from an `Output = Never` workflow
88+
let boolOutputWorkflow: AnyWorkflow<String, Bool> = NeverOutputWorkflow().mapOutput(to: Bool.self)
89+
90+
XCTAssertNotNil(boolOutputWorkflow)
91+
}
8592
}
8693

8794
/// Has no state or output, simply renders a reversed string
@@ -150,3 +157,13 @@ private struct OnOutputChildWorkflow: Workflow {
150157
}
151158
}
152159
}
160+
161+
private struct NeverOutputWorkflow: Workflow {
162+
typealias Output = Never
163+
typealias Rendering = String
164+
typealias State = Void
165+
166+
func render(state: State, context: RenderContext<NeverOutputWorkflow>) -> String {
167+
"Never"
168+
}
169+
}

0 commit comments

Comments
 (0)