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

Support using @Default in @Observable classes #142

Open
sindresorhus opened this issue Jun 7, 2023 · 2 comments
Open

Support using @Default in @Observable classes #142

sindresorhus opened this issue Jun 7, 2023 · 2 comments

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented Jun 7, 2023

I think we can finally support @Default in observable classes.

More info: https://github.com/apple/swift-evolution/blob/main/proposals/0395-observability.md

@Obserable expands class properties to:

var order: Order? {
    get { 
        self.access(keyPath: \.order)
        return _order
    }
    set {
        self.withMutation(keyPath: \.order) {
            _order = newValue
        }
    }
}

So we could make a macro that could be attached a property and it would generate similar code, but also integrate with Defaults.

It would be nice if @Default could be reused, but I'm not sure whether it's possible.

I'm open to other ideas on how to make us able to integrate Defaults into @Observable classes.

There is no rush with this one. We will probably not do anything about it until after iOS 17 is out.

After iOS 17 is out, we could also maybe try to expand @AppStorage and see if it's a macro then, and if so, what it generates.

@staticdreams
Copy link

Any updates on this? iOS17 is just around the corner and I'm in the process of migrating @ObservableObject to Observable

@sindresorhus
Copy link
Owner Author

This will unfortunately not happen soon unless someone contributes the functionality. Creating a macro is not easy, and the macro system is still in flux. Even the built-in @AppStorage does not yet work with @Observable.

For now, you could use either of these packages. They work with Defaults if you use the same key.

kevinrpb added a commit to kevinrpb/Defaults that referenced this issue Sep 15, 2024
This addresses sindresorhus#142 by creating a new macro that can be used inside
`@Observable` classes.

The macro is implemented in a new `DefaultsMacros` module. The
decision to do so is based on the introduction of a new dependency on
`swift-syntax`, and to keep the main module dependency-free.
kevinrpb added a commit to kevinrpb/Defaults that referenced this issue Nov 19, 2024
This addresses sindresorhus#142 by creating a new macro that can be used inside
`@Observable` classes.

The macro is implemented in a new `DefaultsMacros` module. The
decision to do so is based on the introduction of a new dependency on
`swift-syntax`, and to keep the main module dependency-free.
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

No branches or pull requests

2 participants