SE-0311: Task-local values

For the read operation, since it's no longer async (and effectful read-only is being reviewed anyway), instead of

Task.local(\.requestID)

we can also do

Task.local.requestID

What'd happen if you have different properties to the same Key

extension TaskLocalValues {
  var session1: Key { fatalError() }
  var session2: Key { fatalError() }
}

...

Task.local(\.session1) // Same storage?
Task.local(\.session2) // Same storage?

What is its interaction with TaskGroup, or anything that spawns child task(s)?

Task.withLocal(\.key, value1) {
  let result = withTaskGroup(of: ...) { group in
    group.spawn {
      // value1
    }

    Task.withLocal(\.key, value2) {
      // value2
      group.spawn {
        // value1 ?
      }
    }
    ...
  }
}

Text-related stuff

In Declaring Task-Local Value,

Keys must be defined in the TaskLocalValues namespace:

I don't think that holds true. This would be valid, right?

struct Key: TaskLocalKey {
  static var defaultValue: String { "default" }
}
extension TaskLocalValues {
  var session: Key { fatalError() }
}

or is there some compiler optimization that taken place there?

Also, there's still this lingering text,

If the Sendable proposal is accepted,

2 Likes