Replace Send struct with a nonescaping closure? #1936
Replies: 4 comments 2 replies
-
Hi @dankinsoid, it would be great if this were possible, but unfortunately I don't see how. The reason we bundle the closure up in a await MainActor.run {
withAnimation {
send(.action)
}
} But, the downside to wrapping the closure in the Swift does have a tool to help with the impedance mismatch between escaping and non-escaping, and it's called So, unless someone has an idea to keep the ergonomics of |
Beta Was this translation helpful? Give feedback.
-
@mbrandonw I see, thank you. .run { send in
await send(.action)
}
.animation(.default) .run { send in
await send(withAnimation { .action }) // I've seen similar code and it works, I suspect this should work too
} |
Beta Was this translation helpful? Give feedback.
-
@mbrandonw Thank you for a such detailed answer! .run { send in
await sendWithAnimation {
send(.action)
}
} |
Beta Was this translation helpful? Give feedback.
-
I have a notice about .run { send in
let _send = send.callAsFunction
someMethodWithCompletion {
_send(action)
}
} |
Beta Was this translation helpful? Give feedback.
-
Now when using
.run { send in ... }
it's possible to usesend
in escaping closures, smth likeIt's wrong to do so, but It's possible and what's more, it's confusing: some developers may get the feeling that
run
is for such cases and takerun
as an analogue ofwithCheckedContinuation
,Observable.create
etc.But if we replace the
Send
with@MainActor @Sendable (Action) -> Void
, then such an incorrect use of thesend
will become impossible.Beta Was this translation helpful? Give feedback.
All reactions