Added assign(to: on: ownership:) operator#30
Added assign(to: on: ownership:) operator#30freak4pc merged 2 commits intoCombineCommunity:masterfrom dsk1306:master
assign(to: on: ownership:) operator#30Conversation
Codecov Report
@@ Coverage Diff @@
## master #30 +/- ##
==========================================
+ Coverage 97.48% 97.60% +0.11%
==========================================
Files 40 42 +2
Lines 2387 2500 +113
==========================================
+ Hits 2327 2440 +113
Misses 60 60
Continue to review full report at Codecov.
|
|
This is great! Thank you so much. I think we had some discussions here around this, and there are two thoughts I had
public extension Publisher where Failure == Never {
func assign<Root: AnyObject>(to keyPath: ReferenceWritableKeyPath<Root, Self.Output>,
on object: Root,
retainment: RetainmentStrategy) -> AnyCancellable {
switch retainment {
case .strong:
return assign(to: keyPath, on: object)
case .weak:
return sink(receiveValue: { [weak object] value in
object?[keyPath: keyPath] = value
})
}
}
}
public enum RetainmentStrategy {
case strong
case weak
}The reasoning for this is that we might need to have a lot of these
Would love your thoughts :) |
|
No concern on my end! I’d love to see something of this form land (I tried back when hah). There’s definitely a need for it in every day Combine code (especially |
|
👍 |
|
Seems good to me! 🎉 |
Sources/Operators/AssignWeakly.swift
Outdated
| @@ -0,0 +1,32 @@ | |||
| // | |||
| // AssignWeakly.swift | |||
There was a problem hiding this comment.
No sure if this should be called AssignWeakly. It's not just about the weakly part.
There was a problem hiding this comment.
I've renamed it to just Assign
|
@jasdev I know you tried, I think the thing remains the same from my angle Does it make sense having a bunch of "new" operators called I'm still divided in this, but leaning towards argument overloading. Do you, as consumers, have a preference around this? Perhaps I ask on twitter to gain some clearer understanding. I see there's some prior art in CXExtensions so that's also something worth considering. |
|
Try making a poll in Twitter 😊 |
Agree that it should be an overload. 👌🏽 For the “bunch of” bit, I’m not too worried about that because the only “subscribers as operators” in Combine are
The only note on my end with the |
|
I wonder how many people would use it 🤔 But I agree, |
assign(to:on:ownership:) operator
|
I squashed and cleaned up a bit @dsk1306. |
freak4pc
left a comment
There was a problem hiding this comment.
Tapped approve instead of changes ;O
|
Ok, updated the missing tests. Let me know what you think and if you want to make any additional changes before pulling this in and cutting the next version @dsk1306. |
|
I think it would be nice to move all ownership tests from |
|
@dsk1306 I'm a bit iffy about this because it fits better into AssignMany than AssignOwnership. There's a bit of a fine line there, so I'm not entirely sure. Don't feel too strongly about it, I guess we can go either way. |
|
@freak4pc I've pushed one more commit with updated unit tests. Feel free to ignore it if you think it's unnecessary. |
| } | ||
|
|
||
| func testWeakOwnership() { | ||
| let initialRetainCount = CFGetRetainCount(self) |
There was a problem hiding this comment.
oh cool, great idea moving these here
assign(to:on:ownership:) operatorassign(to: on: ownership:) operator

Using
assign(to:)might produce a memory leak because it creates a strong reference toobject(there is a great topic on swift forums about this). I think it might be useful to haveassignWeakly(to:)operator as part of CombineExt.