pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.22k stars 1.42k forks source link

Compile error about ambiguous ViewStore when reducer uses BindingState/BindableAction #2275

Closed johankool closed 1 year ago

johankool commented 1 year ago

Description

When a reducer contains BindingState/BindableAction but a SwiftUI view doesn't use this, the compiler complains about ambiguous init. (The use case for this may be that this SwiftUI view doesn't need the binding, but a child view does.)

I've written a test that shows this here: https://github.com/johankool/swift-composable-architecture/tree/ambiguous_viewstore

Checklist

Expected behavior

It compiles just fine.

Actual behavior

Ambiguous use of 'init(_:observe:content:file:line:)'

Steps to reproduce

 struct BindableReducer: Reducer {
      struct State: Equatable {
        @BindingState var something: Int
      }

      enum Action: BindableAction {
        case binding(BindingAction<State>)
      }

      var body: some Reducer<State, Action> {
        BindingReducer()
      }
    }

    struct SomeView: View {
      let store: StoreOf<BindableReducer>

      struct ViewState: Equatable {

      }

      var body: some View {
        WithViewStore(store, observe: { _ in ViewState() }) { viewstore in // Ambiguous use of 'init(_:observe:content:file:line:)'
          EmptyView()
        }
      }
    }

The Composable Architecture version information

a4d371e8adc9d081d616f988fb0f089b479e9553 (current prerelease/1.0 branch)

Destination operating system

iOS 16.4

Xcode version information

Version 14.3.1 (14E300c)

Swift Compiler version information

> xcrun swiftc --version
swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.10 clang-1500.0.29.1)
Target: arm64-apple-macosx13.0
stephencelis commented 1 year ago

Thanks! I've cherry picked your test in and PR'd a fix: #2278.