# Function Builder and Type Inference

**URL:** <https://forums.swift.org/t/function-builder-and-type-inference/45355>\
**Category:** Using Swift\
**Tags:** function-builder\
**Created:** [March 5, 2021, 1:40pm UTC](https://forums.swift.org/t/function-builder-and-type-inference/45355 "2021-03-05T13:40:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtsrodrigues](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mtsrodrigues/32/34169_2.png) [@mtsrodrigues](https://forums.swift.org/u/mtsrodrigues)\
**Post date:** [March 5, 2021, 1:40pm UTC](https://forums.swift.org/t/function-builder-and-type-inference/45355/1 "2021-03-05T13:40:29Z")

</div>

Is there any way to make type interference work there? If I use and `Array` of `Columns` the type of `Item` is inferred but not when I use a `ColumnBuilder`.

```swift

struct ContentView: View {
    
    @State var people = (1...100).map { Person(name: "Person \($0)", age: 20) }
    
    var body: some View { 
        Table(items: people) { // Cannot convert value of type '() -> Array<Column<_>>' to expected argument type '() -> [Column<Person>]'
            Column(title: "C1") {
                Text("\($0.name)")
            }
            Column(title: "C2") {
                Text("\($0.age)")
            }
        }
    }
    
}

struct Table<Item: Identifiable> {
    
    let columns: [Column<Item>]
    let items: [Item]
    
    init(items: [Item], @ColumnBuilder columns: () -> [Column<Item>]) {
        self.items = items
        self.columns = columns()
    }
}
 
@_functionBuilder
struct ColumnBuilder {
    static func buildBlock<Item>(_ columns: Column<Item>...) -> [Column<Item>] {
        return columns
    }
}

struct Column<Item> {
    
    let title: String
    let content: (Item) -> (AnyView)
    
    init<Content: View>(title: String, content: @escaping (Item) -> Content) {
        self.title = title
        self.content = { AnyView(content($0)) }
    }
    
}

```

---

<div class="post-metadata">

**Author:** ![xAlien95](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xalien95/32/12558_2.png) [@xAlien95](https://forums.swift.org/u/xAlien95)\
**Post date:** [March 5, 2021, 3:10pm UTC](https://forums.swift.org/t/function-builder-and-type-inference/45355/2 "2021-03-05T15:10:28Z")

</div>

You can make `ColumnBuilder` generic over the `Item` type and use `buildExpression`:

```swift
@resultBuilder
struct ColumnBuilder<Item> {
  static func buildBlock(_ columns: Column<Item>...) -> [Column<Item>] {
    return columns
  }
  
  static func buildExpression(_ column: Column<Item>) -> Column<Item> {
    return column
  }
}

```

```swift
struct Table<Item: Identifiable>: View {
  let columns: [Column<Item>]
  let items: [Item]
  
  init(items: [Item], @ColumnBuilder<Item> columns: () -> [Column<Item>]) {
    self.items = items
    self.columns = columns()
  }
  
  var body: some View { ... }
}

```

As for why `buildExpression` helps the type checker, it is mentioned in [SE-0289](https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md#result-building-methods):

> [@](#):
>
> It is optional, but when provided it allows a result builder to distinguish `Expression` types from `Component` types or _to provide contextual type information for statement-expressions_.

* * *

Side note: it would be nice to have a ready-to-be-executed code snippet. In this case the `Person` struct is missing and both `Column` and `Table` aren't conforming to `View`, so in order to check if there were other errors I needed to "fill the gaps" prior doing anything else 🙂

---

<div class="post-metadata">

**Author:** ![mtsrodrigues](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mtsrodrigues/32/34169_2.png) [@mtsrodrigues](https://forums.swift.org/u/mtsrodrigues)\
**Post date:** [March 5, 2021, 3:19pm UTC](https://forums.swift.org/t/function-builder-and-type-inference/45355/3 "2021-03-05T15:19:37Z")

</div>

> [@xAlien95](#):
>
> As for why `buildExpression` helps the type checker, it is mentioned in [SE-0289](https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md#result-building-methods):

Thank you so much! I tried this approach but I missed `buildExpression`.

> [@xAlien95](#):
>
> Side note: it would be nice to have a ready-to-be-executed code snippet. In this case the `Person` struct is missing and both `Column` and `Table` aren't conforming to `View` , so in order to check if there were other errors I needed to "fill the gaps" prior doing anything else 🙂

Sorry about that 😅
