Skip to content

Improve reducer builder inference and prepare for Swift 5.8 #1863

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 3 commits into from
Jan 25, 2023
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
7 changes: 7 additions & 0 deletions Sources/ComposableArchitecture/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import XCTestDynamicOverlay

// MARK: - Deprecated after 0.49.2

@available(
*,
deprecated,
message: "Use 'ReducerBuilder<_, _>' with explicit 'State' and 'Action' generics, instead."
)
public typealias ReducerBuilderOf<R: ReducerProtocol> = ReducerBuilder<R.State, R.Action>

// NB: As of Swift 5.7, property wrapper deprecations are not diagnosed, so we may want to keep this
// deprecation around for now:
// https://github.com/apple/swift/issues/63139
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ extension Reduce {
"""
)
extension AnyReducer {
public init<R: ReducerProtocol>(@ReducerBuilderOf<R> _ build: @escaping (Environment) -> R)
where R.State == State, R.Action == Action {
public init<R: ReducerProtocol>(
@ReducerBuilder<State, Action> _ build: @escaping (Environment) -> R
) where R.State == State, R.Action == Action {
self.init { state, action, environment in
build(environment).reduce(into: &state, action: action)
}
Expand Down
193 changes: 65 additions & 128 deletions Sources/ComposableArchitecture/Reducer/ReducerBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,82 @@
/// See ``CombineReducers`` for an entry point into a reducer builder context.
@resultBuilder
public enum ReducerBuilder<State, Action> {
#if swift(>=5.7)
@inlinable
public static func buildArray(
_ reducers: [some ReducerProtocol<State, Action>]
) -> some ReducerProtocol<State, Action> {
_SequenceMany(reducers: reducers)
}

@inlinable
public static func buildBlock() -> some ReducerProtocol<State, Action> {
EmptyReducer()
}

@inlinable
public static func buildBlock(
_ reducer: some ReducerProtocol<State, Action>
) -> some ReducerProtocol<State, Action> {
reducer
}
@inlinable
public static func buildArray<R: ReducerProtocol>(_ reducers: [R]) -> _SequenceMany<R>
where R.State == State, R.Action == Action {
_SequenceMany(reducers: reducers)
}

@inlinable
public static func buildEither<R0: ReducerProtocol, R1: ReducerProtocol>(
first reducer: R0
) -> _Conditional<R0, R1>
where R0.State == State, R0.Action == Action, R1.State == State, R1.Action == Action {
.first(reducer)
}
@inlinable
public static func buildBlock() -> EmptyReducer<State, Action> {
EmptyReducer()
}

@inlinable
public static func buildEither<R0: ReducerProtocol, R1: ReducerProtocol>(
second reducer: R1
) -> _Conditional<R0, R1>
where R0.State == State, R0.Action == Action, R1.State == State, R1.Action == Action {
.second(reducer)
}
@inlinable
public static func buildBlock<R: ReducerProtocol>(_ reducer: R) -> R
where R.State == State, R.Action == Action {
reducer
}

@inlinable
public static func buildExpression(
_ expression: some ReducerProtocol<State, Action>
) -> some ReducerProtocol<State, Action> {
expression
}
@inlinable
public static func buildEither<R0: ReducerProtocol, R1: ReducerProtocol>(
first reducer: R0
) -> _Conditional<R0, R1>
where R0.State == State, R0.Action == Action, R1.State == State, R1.Action == Action {
.first(reducer)
}

@inlinable
public static func buildFinalResult(
_ reducer: some ReducerProtocol<State, Action>
) -> some ReducerProtocol<State, Action> {
reducer
}
@inlinable
public static func buildEither<R0: ReducerProtocol, R1: ReducerProtocol>(
second reducer: R1
) -> _Conditional<R0, R1>
where R0.State == State, R0.Action == Action, R1.State == State, R1.Action == Action {
.second(reducer)
}

@inlinable
public static func buildLimitedAvailability(
_ wrapped: some ReducerProtocol<State, Action>
) -> Reduce<State, Action> {
Reduce(wrapped)
}
@inlinable
public static func buildExpression<R: ReducerProtocol>(_ expression: R) -> R
where R.State == State, R.Action == Action {
expression
}

@inlinable
public static func buildOptional(
_ wrapped: (some ReducerProtocol<State, Action>)?
) -> some ReducerProtocol<State, Action> {
wrapped
}
@inlinable
public static func buildFinalResult<R: ReducerProtocol>(_ reducer: R) -> R
where R.State == State, R.Action == Action {
reducer
}

@inlinable
public static func buildPartialBlock(
first: some ReducerProtocol<State, Action>
) -> some ReducerProtocol<State, Action> {
first
}
@inlinable
public static func buildLimitedAvailability<R: ReducerProtocol>(
_ wrapped: R
) -> Reduce<State, Action>
where R.State == State, R.Action == Action {
Reduce(wrapped)
}

@inlinable
public static func buildPartialBlock(
accumulated: some ReducerProtocol<State, Action>, next: some ReducerProtocol<State, Action>
) -> some ReducerProtocol<State, Action> {
_Sequence(accumulated, next)
}
#else
@inlinable
public static func buildArray<R: ReducerProtocol>(_ reducers: [R]) -> _SequenceMany<R>
where R.State == State, R.Action == Action {
_SequenceMany(reducers: reducers)
}
@inlinable
public static func buildOptional<R: ReducerProtocol>(_ wrapped: R?) -> R?
where R.State == State, R.Action == Action {
wrapped
}

@inlinable
public static func buildBlock() -> EmptyReducer<State, Action> {
EmptyReducer()
}
@inlinable
public static func buildPartialBlock<R: ReducerProtocol>(
first: R
) -> R
where R.State == State, R.Action == Action {
first
}

@inlinable
public static func buildBlock<R: ReducerProtocol>(_ reducer: R) -> R
where R.State == State, R.Action == Action {
reducer
}
@inlinable
public static func buildPartialBlock<R0: ReducerProtocol, R1: ReducerProtocol>(
accumulated: R0, next: R1
) -> _Sequence<R0, R1>
where R0.State == State, R0.Action == Action, R1.State == State, R1.Action == Action {
_Sequence(accumulated, next)
}

#if swift(<5.7)
@inlinable
public static func buildBlock<
R0: ReducerProtocol,
Expand Down Expand Up @@ -330,54 +311,12 @@ public enum ReducerBuilder<State, Action> {
)
}

@inlinable
public static func buildEither<R0: ReducerProtocol, R1: ReducerProtocol>(
first reducer: R0
) -> _Conditional<R0, R1>
where R0.State == State, R0.Action == Action {
.first(reducer)
}

@inlinable
public static func buildEither<R0: ReducerProtocol, R1: ReducerProtocol>(
second reducer: R1
) -> _Conditional<R0, R1>
where R1.State == State, R1.Action == Action {
.second(reducer)
}

@inlinable
public static func buildExpression<R: ReducerProtocol>(_ expression: R) -> R
where R.State == State, R.Action == Action {
expression
}

@inlinable
public static func buildFinalResult<R: ReducerProtocol>(_ reducer: R) -> R
where R.State == State, R.Action == Action {
reducer
}

@_disfavoredOverload
@inlinable
public static func buildFinalResult<R: ReducerProtocol>(_ reducer: R) -> Reduce<State, Action>
where R.State == State, R.Action == Action {
Reduce(reducer)
}

@inlinable
public static func buildLimitedAvailability<R: ReducerProtocol>(
_ wrapped: R
) -> Reduce<R.State, R.Action>
where R.State == State, R.Action == Action {
Reduce(wrapped)
}

@inlinable
public static func buildOptional<R: ReducerProtocol>(_ wrapped: R?) -> R?
where R.State == State, R.Action == Action {
wrapped
}
#endif

public enum _Conditional<First: ReducerProtocol, Second: ReducerProtocol>: ReducerProtocol
Expand Down Expand Up @@ -440,5 +379,3 @@ public enum ReducerBuilder<State, Action> {
}
}
}

public typealias ReducerBuilderOf<R: ReducerProtocol> = ReducerBuilder<R.State, R.Action>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
/// .ifLet(\.child, action: /Action.child)
/// }
/// ```
public struct CombineReducers<Reducers: ReducerProtocol>: ReducerProtocol {
public struct CombineReducers<State, Action, Reducers: ReducerProtocol>: ReducerProtocol
where State == Reducers.State, Action == Reducers.Action {
@usableFromInline
let reducers: Reducers

Expand All @@ -24,7 +25,7 @@ public struct CombineReducers<Reducers: ReducerProtocol>: ReducerProtocol {
/// - Parameter build: A reducer builder.
@inlinable
public init(
@ReducerBuilderOf<Reducers> _ build: () -> Reducers
@ReducerBuilder<State, Action> _ build: () -> Reducers
) {
self.init(internal: build())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ extension ReducerProtocol {
/// state.
/// - Returns: A reducer that combines the child reducer with the parent reducer.
@inlinable
public func forEach<ID: Hashable, Element: ReducerProtocol>(
_ toElementsState: WritableKeyPath<State, IdentifiedArray<ID, Element.State>>,
action toElementAction: CasePath<Action, (ID, Element.Action)>,
@ReducerBuilderOf<Element> _ element: () -> Element,
public func forEach<ElementState, ElementAction, ID: Hashable, Element: ReducerProtocol>(
_ toElementsState: WritableKeyPath<State, IdentifiedArray<ID, ElementState>>,
action toElementAction: CasePath<Action, (ID, ElementAction)>,
@ReducerBuilder<ElementState, ElementAction> _ element: () -> Element,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _ForEachReducer<Self, ID, Element> {
) -> _ForEachReducer<Self, ID, Element>
where ElementState == Element.State, ElementAction == Element.Action {
_ForEachReducer(
parent: self,
toElementsState: toElementsState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ extension ReducerProtocol {
/// present
/// - Returns: A reducer that combines the child reducer with the parent reducer.
@inlinable
public func ifCaseLet<Case: ReducerProtocol>(
_ toCaseState: CasePath<State, Case.State>,
action toCaseAction: CasePath<Action, Case.Action>,
@ReducerBuilderOf<Case> then case: () -> Case,
public func ifCaseLet<CaseState, CaseAction, Case: ReducerProtocol>(
_ toCaseState: CasePath<State, CaseState>,
action toCaseAction: CasePath<Action, CaseAction>,
@ReducerBuilder<CaseState, CaseAction> then case: () -> Case,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _IfCaseLetReducer<Self, Case> {
) -> _IfCaseLetReducer<Self, Case>
where CaseState == Case.State, CaseAction == Case.Action {
.init(
parent: self,
child: `case`(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ extension ReducerProtocol {
/// state.
/// - Returns: A reducer that combines the child reducer with the parent reducer.
@inlinable
public func ifLet<Wrapped: ReducerProtocol>(
_ toWrappedState: WritableKeyPath<State, Wrapped.State?>,
action toWrappedAction: CasePath<Action, Wrapped.Action>,
@ReducerBuilderOf<Wrapped> then wrapped: () -> Wrapped,
public func ifLet<WrappedState, WrappedAction, Wrapped: ReducerProtocol>(
_ toWrappedState: WritableKeyPath<State, WrappedState?>,
action toWrappedAction: CasePath<Action, WrappedAction>,
@ReducerBuilder<WrappedState, WrappedAction> then wrapped: () -> Wrapped,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _IfLetReducer<Self, Wrapped> {
) -> _IfLetReducer<Self, Wrapped>
where WrappedState == Wrapped.State, WrappedAction == Wrapped.Action {
.init(
parent: self,
child: wrapped(),
Expand Down
20 changes: 10 additions & 10 deletions Sources/ComposableArchitecture/Reducer/Reducers/Scope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ public struct Scope<ParentState, ParentAction, Child: ReducerProtocol>: ReducerP
/// - toChildAction: A case path from parent action to a case containing child actions.
/// - child: A reducer that will be invoked with child actions against child state.
@inlinable
public init(
state toChildState: WritableKeyPath<ParentState, Child.State>,
action toChildAction: CasePath<ParentAction, Child.Action>,
@ReducerBuilderOf<Child> _ child: () -> Child
) {
public init<ChildState, ChildAction>(
state toChildState: WritableKeyPath<ParentState, ChildState>,
action toChildAction: CasePath<ParentAction, ChildAction>,
@ReducerBuilder<ChildState, ChildAction> _ child: () -> Child
) where ChildState == Child.State, ChildAction == Child.Action {
self.init(
toChildState: .keyPath(toChildState),
toChildAction: toChildAction,
Expand Down Expand Up @@ -215,14 +215,14 @@ public struct Scope<ParentState, ParentAction, Child: ReducerProtocol>: ReducerP
/// - toChildAction: A case path from parent action to a case containing child actions.
/// - child: A reducer that will be invoked with child actions against child state.
@inlinable
public init(
state toChildState: CasePath<ParentState, Child.State>,
action toChildAction: CasePath<ParentAction, Child.Action>,
@ReducerBuilderOf<Child> _ child: () -> Child,
public init<ChildState, ChildAction>(
state toChildState: CasePath<ParentState, ChildState>,
action toChildAction: CasePath<ParentAction, ChildAction>,
@ReducerBuilder<ChildState, ChildAction> _ child: () -> Child,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) {
) where ChildState == Child.State, ChildAction == Child.Action {
self.init(
toChildState: .casePath(toChildState, file: file, fileID: fileID, line: line),
toChildAction: toChildAction,
Expand Down
4 changes: 2 additions & 2 deletions Tests/ComposableArchitectureTests/ForEachReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct Elements: ReducerProtocol {
}
#if swift(>=5.7)
var body: some ReducerProtocol<State, Action> {
Reduce<State, Action> { state, action in
Reduce { state, action in
.none
}
.forEach(\.rows, action: /Action.row) {
Expand All @@ -99,7 +99,7 @@ struct Elements: ReducerProtocol {
}
#else
var body: Reduce<State, Action> {
Reduce<State, Action> { state, action in
Reduce { state, action in
.none
}
.forEach(\.rows, action: /Action.row) {
Expand Down