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

Disfavor shared optional dynamic member lookup #3170

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,59 @@ references:
)
)
```

## Migrating to 1.11.1

A few bug fixes landed in 1.11.1 that may be source breaking. They are described below:

### `withLock` is now `@MainActor`

In [version 1.11](<doc:MigratingTo1.11>) of the library we deprecated mutating shared state from
asynchronous contexts, such as effects, and instead recommended using the new
``Shared/withLock(_:)`` method. Doing so made it possible to lock all mutations to the shared state
and prevent race conditions (see the [migration guide](<doc:MigratingTo1.11>) for more info).

However, this did leave open the possibility for deadlocks if shared state was read from and written
to on different threads. To fix this we have now restricted ``Shared/withLock(_:)`` to the
`@MainActor`, and so you will now need to `await` its usage:

```diff
-sharedCount.withLock { $0 += 1 }
+await sharedCount.withLock { $0 += 1 }
```

The compiler should suggest this fix-it for you.

### Optional dynamic member lookup on `Shared` is deprecated/disfavored

When the ``Shared`` property wrapper was first introduced, its dynamic member lookup was overloaded
to automatically unwrap optionals for ergonomic purposes:

```swift
if let sharedUnwrappedProperty = $shared.optionalProperty {
// ...
}
```

This unfortunately made dynamic member lookup a little more difficult to understand:

```swift
$shared.optionalProperty // Shared<Value>?, *not* Shared<Value?>
```

…and required casting and other tricks to transform shared values into what one might expect.

And so this dynamic member lookup is deprecated and has been disfavored, and will eventually be
removed entirely. Instead, you can use ``Shared/init(_:)`` to explicitly unwrap a shared optional
value.

Disfavoring it does have the consequence of being source breaking in the case of `if let` and
`guard let` expressions, where Swift does not select the optional overload automatically. To
migrate, use ``Shared/init(_:)``:

```diff
-if let sharedUnwrappedProperty = $shared.optionalProperty {
+if let sharedUnwrappedProperty = Shared($shared.optionalProperty) {
// ...
}
```

This file was deleted.

2 changes: 2 additions & 0 deletions Sources/ComposableArchitecture/SharedState/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public struct Shared<Value> {
Shared<Member>(reference: self.reference, keyPath: self.keyPath.appending(path: keyPath)!)
}

@_disfavoredOverload
@available(
*, deprecated, message: "Use 'Shared($value.optional)' to unwrap optional shared values"
)
Expand Down Expand Up @@ -434,6 +435,7 @@ extension Shared {
SharedReader(reference: self.reference, keyPath: self.keyPath)
}

@_disfavoredOverload
@available(
*, deprecated, message: "Use 'SharedReader($value.optional)' to unwrap optional shared values"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public struct SharedReader<Value> {
SharedReader<Member>(reference: self.reference, keyPath: self.keyPath.appending(path: keyPath)!)
}

@_disfavoredOverload
@available(
*, deprecated, message: "Use 'SharedReader($value.optional)' to unwrap optional shared values"
)
Expand Down