# Boolean operators and \`async let\`

**URL:** <https://forums.swift.org/t/boolean-operators-and-async-let/61354>\
**Category:** Using Swift\
**Tags:** concurrency, async-let\
**Created:** [November 10, 2022, 7:52pm UTC](https://forums.swift.org/t/boolean-operators-and-async-let/61354 "2022-11-10T19:52:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [November 10, 2022, 7:52pm UTC](https://forums.swift.org/t/boolean-operators-and-async-let/61354/1 "2022-11-10T19:52:18Z")

</div>

Why can `async let` variables not be used on the RHS of boolean operators?

```swift
async let first: Int[] = someAsyncCall()
async let second: Int[] = anotherAsyncCall()

let bothEmpty = (await first).isEmpty && (await second).isEmpty
// or
let bothEmpty = await (first.isEmpty && second.isEmpty)
// or any other variation I can think of

```

I always get the errors

> 'async let' in an autoclosure that does not support concurrency

> Capturing 'async let' variables is not supported

I know that `&&` and `||` use `@autoclosure` to implement short-circuit evaluation, but the error is rather opaque; I couldn't find good information on either one on Google.

What's a good way to work around this? Do I have to use an explicit task group?

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [November 10, 2022, 8:16pm UTC](https://forums.swift.org/t/boolean-operators-and-async-let/61354/2 "2022-11-10T20:16:02Z")

</div>

As you noted, Boolean operators are defined with the `rhs` parameter marked as an autoclosure, which means that the expression on the right-hand side is automatically bundled up into a closure, delaying its execution until necessary. Notably, this `rhs` closure property _isn't_ marked as `async` ,so no `await` statements can appear "inside" it.

As for a workaround — you should be able to put both into an array and test them at the same time:

```swift
let bothEmpty = await [first, second].allSatisfy(\.isEmpty)

```

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [November 10, 2022, 8:39pm UTC](https://forums.swift.org/t/boolean-operators-and-async-let/61354/3 "2022-11-10T20:39:44Z")

</div>

> [@nnnnnnnn](#):
>
> Notably, this `rhs` closure property _isn't_ marked as `async` ,so no `await` statements can appear "inside" it.

Given that, I expected this to work:

```swift
func &&(
    lhs: Bool,
    rhs: @autoclosure () async throws -> Bool
) async rethrows -> Bool {
    if lhs {
        return try await rhs
    } else {
        return false
    }
}

```

While that does get rid of one of the errors, it still complains about capturing the `async let` variable. In fact, I couldn't find any overload of `&&` that would definitely work. Making the second argument of type `Bool` would shadow the standard library operator. This:

```swift
@_disfavoredOverload
func &&(lhs: Bool, rhs: Bool) -> Bool {
    return Swift.&&(lhs: lhs, rhs: rhs)
}

```

and similar complain about various things (apparently you can't qualify operators). The only way I could find is to wrap it in a local thunk:

```swift
let and: (Bool, Bool) -> _ = { $0 && $1 }
let bothEmpty = await and(first.isEmpty, second.isEmpty)

```

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [November 10, 2022, 9:33pm UTC](https://forums.swift.org/t/boolean-operators-and-async-let/61354/4 "2022-11-10T21:33:35Z")

</div>

Here's a somewhat more readable solution:

```swift
async let first: Int[] = someAsyncCall()
async let second: Int[] = anotherAsyncCall()
let (firstIsEmpty, secondIsEmpty) = await (first.isEmpty, second.isEmpty)
let bothEmpty = firstIsEmpty && secondIsEmpty

```
