CombineExt icon indicating copy to clipboard operation
CombineExt copied to clipboard

.just, .empty and .fail on AnyPublisher

Open stefanomondino opened this issue 1 year ago • 1 comments

I was wondering if there was a reason to not include in this (awesome) library some utility methods to simplify usage of Just, Fail and Empty, which requires an eraseToAnyPublisher() in so many scenarios.

I was thinking about something like this

public extension AnyPublisher {
    static func just(_ value: Output) -> AnyPublisher<Output, Failure> {
        Just(value)
            .setFailureType(to: Failure.self)
            .eraseToAnyPublisher()
    }
    static func empty() -> AnyPublisher<Output, Failure> {
        Empty()
            .setFailureType(to: Failure.self)
            .eraseToAnyPublisher()
    }
    
    static func fail(_ error: Failure) -> AnyPublisher<Output, Failure> {
        Fail(error: error).eraseToAnyPublisher()
    }
}

this could allow something like

func somePublisherValue() -> AnyPublisher<String, Never> {
.just("hey")
// instead of Just("hey").eraseToAnyPublisher()
}

This should also be super-useful in complex flatMaps with AnyPublishers as return values and some guard/let that always returns a Just/Fail

Is it something that could be useful or it's pointless? Am I missing something? Is it already there and I can't see it? :D

Cheers and thanks (as usual) for the awesome work.

stefanomondino avatar Apr 14 '23 10:04 stefanomondino

This could be really useful!

Oni-zerone avatar Apr 20 '23 08:04 Oni-zerone