Skip to content
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

[Breaking] Unlock Self for observable types #7448

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jsflax
Copy link
Contributor

@jsflax jsflax commented Sep 22, 2021

This PR does a number of things. The downside of these things is that observe will be less discoverable in our docs, but it does consolidate a lot of code, makes our API more type safe, and removes functionality that didn't really work:

  • Disallow key path observation on collections where the Element is not of Object type. Attempting to observe keyPaths on non-object types yields no changes, so this functionality was already broken.
    • @tgoyne I am not sure how we feel about this. It breaks the API for those that were using it incorrectly.
  • Unlock Self for observable types. By moving object observation behind a private protocol, and collection observation
    cleaned up, key path filtering and Object/CollectionChanges have become more type safe. Previously, keyPath filtering
    was called as:
person.observe(keyPaths: [\Person.name, \Person.age]) { change in /* do stuff */ }

Now the class can be dropped, as it is derived from the calling type:

person.observe(keyPaths: [\.name, \.age]) { change in /* do stuff */ }

Non-keyPath filtering has also been enhanced. Previously:

person.observe { change in
    switch change {
    case .initial(let object):
  	// type is of ObjectBase and needs to be casted
  	guard let person = object as? Person else { fatalError() }
  	// do stuff
    default: break
    }
}

Now:

person.observe { change in
    switch change {
    case .initial(let person):
  	// person is already of `Person` type
    }
}

The above is all unlocked for collection observation as well.

  • Consolidate observation logic to RealmCollection.

@jsflax
Copy link
Contributor Author

jsflax commented Sep 23, 2021

According to the CI, this seems to fail for Xcode < 13. I will have to investigate and potentially guard this behind directives.

@jsflax
Copy link
Contributor Author

jsflax commented Sep 23, 2021

Reproduced the compiler error on Swift 5.4. It would appear https://github.com/apple/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md enables the Self behaviour that this PR unlocks. So users on Xcode < 13 will not be able to take advantage of the generic type lookup for keyPath filtering, but they can still take advantage of Object/CollectionChanges passing back the fully typed object.

@bmunkholm bmunkholm changed the title Unlock Self for observable types [Breaking] Unlock Self for observable types Feb 22, 2022
@bmunkholm bmunkholm marked this pull request as draft May 19, 2022 14:56
@tgoyne tgoyne removed their request for review May 22, 2023 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants