Recursion in a SwiftUI Table

Using SwiftData for a MacOS app I have a working Table but I'd like to add recursion to the rows so instead of stopping at the first child level I can get to grandchildren, etc. Tried creating a separate func that calls itself but cannot get working syntax, possibly because this is happening inside a DisclosureTableRow or I just have bad syntax. Instead of calling TableRow(childAccount) I'd like to be able to call a recursive function which determines if each child is treated as a parent or not until there are no more grandchildren.

Each Account class object has .parent: Account, .children: [Account], and .isExpanded:Bool (required by the DisclosureTableRow but not changed here). This is the working non-recursive code:

                  ForEach(theAccounts.sorted(using: sortOrder)) { account in
                    // Make this account bindable so can use .isExpanded directly
                    @Bindable var account = account
                    // Check if the account is not a child of any other account to avoid duplcates,
                    if !theAssetAccounts.contains(where: { $0.children?.contains(account) ?? false }) {
                      // If the account has children, display them in a DisclosureTableRow…
                      if let children = account.children, !children.isEmpty {
                        DisclosureTableRow(account, isExpanded: $account.isExpanded) {
                          ForEach(children) { childAccount in
                            TableRow(childAccount)
                          }
                        }
                      } else {
                        // …or if the account has no children, display it in a simple TableRow
                        TableRow(account)
                      }
                    }
                  }
                }

First the singleton theMotherAccount is at the top level then we iterate over an array of other accounts, theAccounts, showing only those that are not themselves children. Any children are then surfaced as part of another DisclosureTableRow.

I thought I could just create a recursive func to return either a DisclosureTableRow or a TableRow but have not been able to find acceptable syntax.

This is what I thought ought to work:

  if let children = account.children, !children.isEmpty {
    return DisclosureTableRow(account, isExpanded: Bindable(account).isExpanded) {
      ForEach(children) { child in
        recursiveAccountRow(account: child)
      }
    }
  } else {
    return TableRow(account)
  }
}

I've not gotten anywhere with this. The issue is how does one abstract code from within a DisclosureTableRow and return either another DisclosureTableRow or a TableRow. Why not organize the accounts hierarchically and feed that to Table? Because then childless parents have disclosure triangles and there are issues with expansion.

Recursion in a SwiftUI Table
 
 
Q