# Nested functions and @ViewBuilder: strange compiler errors

**URL:** <https://forums.swift.org/t/nested-functions-and-viewbuilder-strange-compiler-errors/66115>\
**Category:** Using Swift\
**Tags:** swiftui, functions, viewbuilder\
**Created:** [July 12, 2023, 4:54pm UTC](https://forums.swift.org/t/nested-functions-and-viewbuilder-strange-compiler-errors/66115 "2023-07-12T16:54:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crontab](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crontab/32/7374_2.png) [@crontab](https://forums.swift.org/u/crontab)\
**Post date:** [July 12, 2023, 4:54pm UTC](https://forums.swift.org/t/nested-functions-and-viewbuilder-strange-compiler-errors/66115/1 "2023-07-12T16:54:28Z")

</div>

The following code gives a strange compiler error that doesn't seem relevant:

```swift
struct ContentView: View {

    var body: some View {

        func world() -> String {
            "world"
        }

        Text("Hello, \(world())!")
    }
}

```

The error message is:

`Closure containing a declaration cannot be used with result builder 'ViewBuilder'`

Interestingly, if I add `return` in `world()` (i.e. `return "world"`) the compiler shows an error elsewhere and also adds a warning, both not being very relevant either. This seems like a compiler bug more than anything else.

Any ideas what's going on?

---

<div class="post-metadata">

**Author:** ![krilnon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krilnon/32/276_2.png) [@krilnon](https://forums.swift.org/u/krilnon)\
**Post date:** [July 12, 2023, 5:57pm UTC](https://forums.swift.org/t/nested-functions-and-viewbuilder-strange-compiler-errors/66115/2 "2023-07-12T17:57:58Z")

</div>

I think it's telling you that nesting a function declaration directly in a result builder isn't supported.

You could try hoisting the definition of `world()` out of `body`, or change it to:

```swift
var world: () -> String = {
    "world"
}

```

---

<div class="post-metadata">

**Author:** ![crontab](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crontab/32/7374_2.png) [@crontab](https://forums.swift.org/u/crontab)\
**Post date:** [July 12, 2023, 6:02pm UTC](https://forums.swift.org/t/nested-functions-and-viewbuilder-strange-compiler-errors/66115/3 "2023-07-12T18:02:15Z")

</div>

There's probably some workaround, but the error message ("Closure containing a declaration...") doesn't seem to be relevant, there's no closure containing a declaration here. Plus just adding `return` makes the compiler say something else completely and point elsewhere, it's why I thought it's a compiler bug.

Besides, why wouldn't nested functions be supported in ViewBuilders?

---

<div class="post-metadata">

**Author:** ![krilnon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krilnon/32/276_2.png) [@krilnon](https://forums.swift.org/u/krilnon)\
**Post date:** [July 12, 2023, 6:13pm UTC](https://forums.swift.org/t/nested-functions-and-viewbuilder-strange-compiler-errors/66115/4 "2023-07-12T18:13:16Z")

</div>

I suspect (but don't know for sure) that the misleading diagnostics are related to implementation details of result builders. I wouldn't be surprised, for example, if a closure is synthesized in the middle somewhere.

> [@crontab](#):
>
> Besides, why wouldn't nested functions be supported in ViewBuilders?

Result builders historically had some limitations on what syntax worked inside of them. Many of those limitations were removed in [SE-0373: Lift all limitations on variables in result builders](https://github.com/apple/swift-evolution/blob/main/proposals/0373-vars-without-limits-in-result-builders.md), but I wouldn't be surprised if some remain.
