Skip to content

Adds a convenience for mapping Never outputs to other types #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Workflow/Sources/AnyWorkflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,14 @@ extension AnyWorkflowConvertible {
asAnyWorkflow().mapRendering(transform)
}
}

// MARK: -

extension AnyWorkflowConvertible where Output == Never {
/// A convenience for workflows that don't produce output but want to change the declared output type.
public func mapOutput<NewOutput>(to: NewOutput.Type) -> AnyWorkflow<Rendering, NewOutput> {
mapOutput(mapNever(_:))
}
}

private func mapNever<T>(_ input: Never) -> T {}
17 changes: 17 additions & 0 deletions Workflow/Tests/AnyWorkflowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public class AnyWorkflowTests: XCTestCase {

XCTAssertNotNil(erased.base as? OnOutputWorkflow)
}

func testMapNeverToOtherType() {
// test that this `Output = Bool` workflow compiles when mapped from an `Output = Never` workflow
let boolOutputWorkflow: AnyWorkflow<String, Bool> = NeverOutputWorkflow().mapOutput(to: Bool.self)

XCTAssertNotNil(boolOutputWorkflow)
}
}

/// Has no state or output, simply renders a reversed string
Expand Down Expand Up @@ -150,3 +157,13 @@ private struct OnOutputChildWorkflow: Workflow {
}
}
}

private struct NeverOutputWorkflow: Workflow {
typealias Output = Never
typealias Rendering = String
typealias State = Void

func render(state: State, context: RenderContext<NeverOutputWorkflow>) -> String {
"Never"
}
}
Loading