Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# master
*Please add new entries at the top.*
1. `promoteError` can now infer the new error type from the context. (#xxx, kudos to @andersio)

1. `promoteErrors(_:)` has been renamed to `promoteError(_:)`. (#408, kudos to @andersio)

# 1.1.3
Expand Down
17 changes: 16 additions & 1 deletion Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ extension Signal where Error == NoError {
/// - _ An `ErrorType`.
///
/// - returns: A signal that has an instantiatable `ErrorType`.
public func promoteError<F: Swift.Error>(_: F.Type) -> Signal<Value, F> {
public func promoteError<F: Swift.Error>(_: F.Type = F.self) -> Signal<Value, F> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

return Signal<Value, F> { observer in
return self.observe { event in
switch event {
Expand All @@ -2422,6 +2422,21 @@ extension Signal where Error == NoError {
}
}

/// Promote a signal that does not generate failures into one that can.
///
/// - note: This does not actually cause failures to be generated for the
/// given signal, but makes it easier to combine with other signals
/// that may fail; for example, with operators like
/// `combineLatestWith`, `zipWith`, `flatten`, etc.
///
/// - parameters:
/// - _ An `ErrorType`.
///
/// - returns: A signal that has an instantiatable `ErrorType`.
public func promoteError(_: Error.Type = Error.self) -> Signal<Value, Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait why is this overload necessary?

Copy link
Member Author

@andersio andersio May 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't necessary, just that it allows one adds promoteError to everything in a brute force manner, while not yielding extra Signals in cases with the identical error types. Say if you do:

SignalProducer<Int, CocoaError>.combineLatest(
    SignalProducer<Int, CocoaError>.empty.promoteError(),
    SignalProducer<Int, NoError>.empty.promoteError()
)

The resulting Signal graph would be equivalent to:

SignalProducer<Int, CocoaError>.combineLatest(
    SignalProducer<Int, CocoaError>.empty,
    SignalProducer<Int, NoError>.empty.promoteError()
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very much 👎 for that. Since when has RAC promoted brainless thinking? :P A user calling this operator when they shouldn't is likely an error and shouldn't be silenced IMO.

Copy link
Member Author

@andersio andersio May 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I should be more careful about my wordings. The overload does not add any capability, and is just a fast path when the compiler knows Self.Error is the Error of the return producer.

Say SignalProducer<Int, CocoaError>.empty.promoteError(CocoaError.self).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh that's right, because it's not possible to do negative conditions on extensions. This is a great idea then 👍

return self
}

/// Forward events from `self` until `interval`. Then if signal isn't
/// completed yet, fails with `error` on `scheduler`.
///
Expand Down
17 changes: 16 additions & 1 deletion Sources/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1397,10 +1397,25 @@ extension SignalProducer where Error == NoError {
/// - _ An `ErrorType`.
///
/// - returns: A producer that has an instantiatable `ErrorType`.
public func promoteError<F: Swift.Error>(_: F.Type) -> SignalProducer<Value, F> {
public func promoteError<F: Swift.Error>(_: F.Type = F.self) -> SignalProducer<Value, F> {
return lift { $0.promoteError(F.self) }
}

/// Promote a producer that does not generate failures into one that can.
///
/// - note: This does not actually cause failers to be generated for the
/// given producer, but makes it easier to combine with other
/// producers that may fail; for example, with operators like
/// `combineLatestWith`, `zipWith`, `flatten`, etc.
///
/// - parameters:
/// - _ An `ErrorType`.
///
/// - returns: A producer that has an instantiatable `ErrorType`.
public func promoteError(_: Error.Type = Error.self) -> SignalProducer<Value, Error> {
return self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

}

/// Forward events from `self` until `interval`. Then if producer isn't
/// completed yet, fails with `error` on `scheduler`.
///
Expand Down