Question about syntax for closures

Suppose I'm building an ASCII version of SwiftUI and I have the following code:

import Foundation

struct AsciiButton {
    private let id = UUID()
    let callback: (UUID) -> Void
    
    var body: String {
        "[ Tap me ]"
    }
}

struct ChildAsciiView {
    let callback: (UUID) -> Void
    
    var body: String {
        AsciiButton(callback: self.callback).body // This works
//        AsciiButton(callback: self.callback(_:)).body  // Doesn't work
    }
}

struct ParentAsciiView {
    var body: String {
        ChildAsciiView(callback: self.callback(_:)).body
    }
    
    private func callback(_ viewID: UUID) {
        print("LOL!!")
    }
}

In ChildAsciiView, I've commented out a line that doesn't work. But why doesn't it work? There is a difference between a function and a closure, but you can use one syntax in ParentAsciiView but not that same syntax in ChildAsciiView. I'm having trouble articulating why exactly.

I believe the answer is that closures don't have argument labels, so the syntax for specifying parameters is not accepted.

2 Likes