CombineCommunity / CombineExt

CombineExt provides a collection of operators, publishers and utilities for Combine, that are not provided by Apple themselves, but are common in other Reactive Frameworks and standards.
https://combine.community
MIT License
1.72k stars 151 forks source link

⚠️ Fix `map(to:)` name collision #128

Closed danshevluk closed 2 years ago

danshevluk commented 2 years ago

Hello everyone!

After the latest update 1.6.0 our project that uses CombineExt stopped compiling with a wired error message. After some digging, I figured out that in some cases Swift confuses map(to:) extension with map operator and uses closure as a constant itself.

It can be reproduced with a test case that I added:

let fooSubject = PassthroughSubject<Int, Never>()
let barSubject = PassthroughSubject<Int, Never>()

let combinedPublisher = Publishers.CombineLatest(fooSubject, barSubject)
   .map { fooItem, barItem in
       fooItem * barItem // Error here: "ambiguous use of operator '*'"
   }
// adding .eraseToAnyPublisher() will produce AnyPublisher<(Int, Int) -> Int, Never>

_ = combinedPublisher
    .map {
        "\($0)"
     }
     .sink {  
         print($0)
     }

fooSubject.send(5)
barSubject.send(6)

One possible solution is to rename this operator to mapToValue. This both makes sense semantically, fixes name collision, and is very similar to mapToResult. Also, added more tests for updated mapToValue

⚠️ Note: this is a breaking change, but it fixes a critical issue.

This is my first PR to CombineExt, please let me know what you think! ✌️

codecov[bot] commented 2 years ago

Codecov Report

Merging #128 (7f482aa) into main (dcfb8a8) will increase coverage by 0.05%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #128      +/-   ##
==========================================
+ Coverage   95.44%   95.50%   +0.05%     
==========================================
  Files          68       68              
  Lines        3735     3781      +46     
==========================================
+ Hits         3565     3611      +46     
  Misses        170      170              
Impacted Files Coverage Δ
Sources/Operators/MapToValue.swift 100.00% <100.00%> (ø)
Tests/MapToValueTests.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update dcfb8a8...7f482aa. Read the comment docs.