Skip to content

Dependency with main actor-isolated initializer requirements like FeedbackGeneratorClient from isowords #22

Answered by tgrapperon
ph1ps asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @ph1ps, you're right, it is indeed really annoying to wrap main actor stuff with the protocol requirement getting in the way.
I have a MainActorIsolated wrapper that I'm toying with to tackle these issues:

public final class MainActorIsolated<Value: AnyObject>: @unchecked Sendable {
  @MainActor
  public private(set) lazy var value: Value = initialValue()
  private let initialValue: @MainActor @Sendable () -> Value
  public init(_ initialValue: @MainActor @Sendable @escaping () -> Value) {
    self.initialValue = initialValue
  }
  
  @MainActor
  public func withValue<T: Sendable>(
    _ operation: @MainActor (inout Value) throws -> T
  ) rethrows -> T {
    return try operation(&value)

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
7 replies
@tgrapperon
Comment options

@ph1ps
Comment options

@tgrapperon
Comment options

@tgrapperon
Comment options

@filiplazov
Comment options

Answer selected by ph1ps
Comment options

You must be logged in to vote
3 replies
@ph1ps
Comment options

@kamcma
Comment options

@kamcma
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants