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

Add support for @Dependency(MyDependency.self) #172

Merged
merged 13 commits into from
Jan 22, 2024
Merged

Conversation

stephencelis
Copy link
Member

@stephencelis stephencelis commented Jan 22, 2024

Now that @Environment supports a shorthand for objects, we should do similar in our library.

This PR allows you to skip the step of extending DependencyValues with a key path and instead rely directly on the dependency key, which usually aligns with the value.

 extension APIClient: DependencyKey {
   var liveValue: Self { … }
 }

-extension DependencyValues {
-  var apiClient: APIClient {
-    get { self[APIClient.self] }
-    set { self[APIClient.self] = newValue }
-  }
-}
-
 class MyModel {
-  @Dependency(\.apiClient) var apiClient
+  @Dependency(APIClient.self) var apiClient

This new API is less discoverable and friendly to autocomplete, but may be appropriate for application code that refers to a singleton type, and can save that bit of boilerplate above.

@@ -58,7 +58,7 @@
/// implementation module.
///
/// See the <doc:LivePreviewTest> article for more information.
public protocol DependencyKey: TestDependencyKey {
public protocol DependencyKey<Value>: TestDependencyKey {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snuck this feature into the branch, but could extract to a separate PR...

@stephencelis stephencelis changed the title Add support for @Dependency(Dependency.self) Add support for @Dependency(MyDependency.self) Jan 22, 2024
@stephencelis stephencelis merged commit 4379eaa into main Jan 22, 2024
8 checks passed
@stephencelis stephencelis deleted the deps-updates branch January 22, 2024 20:15
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.

None yet

2 participants