# Cannot infer type returned by previous closure

**URL:** <https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218>\
**Category:** Using Swift\
**Tags:** generics, closure, inout\
**Created:** [August 28, 2024, 4:35pm UTC](https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218 "2024-08-28T16:35:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![antoniopantaleo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/antoniopantaleo/32/37074_2.png) [@antoniopantaleo](https://forums.swift.org/u/antoniopantaleo)\
**Post date:** [August 28, 2024, 4:35pm UTC](https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218/1 "2024-08-28T16:35:02Z")

</div>

I wrote a simple function I wanted to use in my tests, to separate test phases into closures:

```swift
func given<T>(
    _ setup: () -> T,
    when: (inout T) -> Void = { _ in },
    then: (T) -> Void
) {
    var sut = setup()
    when(&sut)
    then(sut)
}

```

but I realized that if the `given` closure is _complex_ in some way the function won't compile:

 ![](https://global.discourse-cdn.com/swift/original/3X/0/7/079a9de789c4246cc43763d5648cf3277ba6e29b.png)

It works if I explicitly write down the type of `T` in the following closures, of I I use one-line `given` closure. It works too if I extract the `given` closure in a separate variable.

Does anybody know why this happens?  
Is there a way swift compiler can infer the `T` returned from `given` closure?

---

<div class="post-metadata">

**Author:** ![bzamayo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bzamayo/32/764_2.png) [@bzamayo](https://forums.swift.org/u/bzamayo)\
**Post date:** [August 28, 2024, 4:53pm UTC](https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218/2 "2024-08-28T16:53:31Z")

</div>

It will only infer if the closure is a single statement. In other cases, the compiler will simply not try. You need to explicitly provide type context somewhere, like `given { _ -> [String : Int] }` for example.

---

<div class="post-metadata">

**Author:** ![Quedlinbug](https://avatars.discourse-cdn.com/v4/letter/q/77aa72/32.png) [@Quedlinbug](https://forums.swift.org/u/Quedlinbug)\
**Post date:** [August 28, 2024, 5:10pm UTC](https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218/3 "2024-08-28T17:10:30Z")

</div>

> [@bzamayo](#):
>
> It will only infer if the closure is a single statement.

This used to be true universally but is [currently only true with `inout` involved](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0326-extending-multi-statement-closure-inference.md#inout-inference-without-a-contextual-type).

```swift
func given<T>(
  _ setup: () -> T,
  when: (T) -> T = { $0 },
  then: (T) -> Void
) {
  then(when(setup()))
}

@Test func gwt_one() {
  given {
    let dict = [String: Int]()
    return dict
  } when: { sut in
    var sut = sut
    sut["a"] = 10
    return sut
  } then: { sut in
    #expect(sut["a"] == 10)
  }
}

```
