Skip to content

Adds Publisher.weaklyAssign(to:on:). #14

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

Closed
wants to merge 2 commits into from
Closed

Adds Publisher.weaklyAssign(to:on:). #14

wants to merge 2 commits into from

Conversation

jasdev
Copy link
Member

@jasdev jasdev commented Apr 3, 2020

Would love any input on naming here—no strong opinions on .weaklyAssign. 👌🏽

Also, GitHub’s parsing of my commit message. 😂

Screen Shot 2020-04-02 at 9 45 32 PM

@codecov
Copy link

codecov bot commented Apr 3, 2020

Codecov Report

Merging #14 into master will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #14      +/-   ##
==========================================
+ Coverage   95.77%   95.82%   +0.05%     
==========================================
  Files          28       30       +2     
  Lines        1253     1270      +17     
==========================================
+ Hits         1200     1217      +17     
  Misses         53       53              
Impacted Files Coverage Δ
Sources/Operators/WeakAssign.swift 100.00% <100.00%> (ø)
Tests/WeakAssignTests.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 83c7150...6beaa52. Read the comment docs.

@freak4pc
Copy link
Member

freak4pc commented Apr 4, 2020

Interesting idea and quite small as well, I'm only trying to think if it's actually useful enough - I'm worried of adding too many "novelty" operators.

I don't want tens of operators called mapWeakly, sinkWeakly, etc. Maybe there's a nice way to generalize it?

@sharplet
Copy link

sharplet commented Apr 8, 2020

Naming-wise, I'm not sure weaklyAssign, weakAssign, etc. are quite right, because it's the object reference that's weak, not the assignment. Following that logic, if feels like the modifier belongs on the object parameter, so perhaps something like this?

publisher.assign(to: \.foo, onWeak: bar)`

This mirrors capture list syntax somewhat, where it would be spelled [weak bar]. It also perhaps lends itself to this variant for unowned:

publisher.assign(to: \.foo, onUnowned: bar)

I'm sympathetic to @freak4pc's point about generality though. Another option I considered was a function that returns a weak reference, e.g.:

final class WeakBox<Object: AnyObject> {
  weak var object: Object?

  init(_ object: Object) {
    self.object = object
  }
}

func weak<Object: AnyObject>(_ object: Object) -> WeakBox<Object> {
  WeakBox(object)
}

publisher.assign(to: \.foo, on: weak(bar))

I don't love it, but I don't hate it. In Bindings I sidestepped this issue altogether by introducing the BindingOwner protocol, which defines weak capture semantics at the type level, rather than in an ad-hoc fashion through operator overloads.

@jasdev
Copy link
Member Author

jasdev commented Apr 8, 2020

@freak4pc, @sharplet: Thank you both for the feedback, means a lot that y’all took the time. 🙏🏽

And totally agree on the ad-hoc’ness of Publisher.weaklyAssign(to:on). I’m guessing the Bindings approach is a bit out of scope for CombineExt—though, Shai can better speak to that 😄. For now, I’ll close this and roll with weak-as-a-function in my projects, since I’m digging how it sort of “promotes” the keyword to a function.

Cheers! 🍻

@jasdev jasdev closed this Apr 8, 2020
@jasdev jasdev deleted the weak-assign branch April 8, 2020 17:48
@jasdev
Copy link
Member Author

jasdev commented Apr 8, 2020

(For any future souls stumbling upon this, wrote up some learnings from trying to implement the WeakBox approach, https://jasdev.me/notes/weak-assignment.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants