Skip to content

Don't include unavailable setter in Swift 5.9. #64

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

Merged
merged 1 commit into from
Jan 6, 2025
Merged

Conversation

mbrandonw
Copy link
Member

Addresses #63.

There seems to be a bug in Swift <=5.10 where the unavailable setter on Shared.wrappedValue makes even the getter unavailable when using dynamic member lookup with subscripts:

// Swift <=5.10
func foo() -> Int {
  @Shared(.inMemory("favorites")) var favorites = [42]
  return favorites[0]  // 🛑 Unavailable setter
}

You can reproduce this with any subscript that has a setter. The solution is to just omit the unavailable setter in Swift <=5.10.

This bug is still present in Swift 6, but to a lesser degree. If you access through the subscript without assign, Swift thinks we are invoking the setter somehow:

// Swift >=6
func foo() {
  @Shared(.inMemory("favorites")) var favorites = [42]
  favorites[0]  // 🛑 Unavailable setter
}

But since there is no real world use case for doing something like this I think we can live with it.

@mbrandonw mbrandonw requested a review from stephencelis January 4, 2025 21:46
Comment on lines +105 to 106
#if compiler(>=6)
public var wrappedValue: Value {
Copy link
Member Author

Choose a reason for hiding this comment

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

It's not possible to surround just the set { … } in the #if check. At least not in Swift 5.10.

@stephencelis stephencelis merged commit dbcd9d4 into main Jan 6, 2025
4 checks passed
@stephencelis stephencelis deleted the fix-swift-5.9 branch January 6, 2025 06:41
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.

2 participants