A
A
Aleksey Pleshkov2017-04-11 13:54:57
Swift
Aleksey Pleshkov, 2017-04-11 13:54:57

How to implement such (example in the full message) code writing in Swift?

Good afternoon. Could you please tell me how this syntax is implemented?

let html = scopes { 
    html {
        body {
            h1 { inner = "hello" }
        }
    }
}

I spied it in the Swifter library ( It seems that all the magic is here )
The implementation itself is very interesting. If you know a link to the documentation, I will be very grateful (well, or a name that you can google).
I just got to this point.
public typealias Closure = (Void) -> Void

public func body(_ c: Closure) {
    print(c)
}

let bo = body {
    text = "test"
}

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
ManWithBear, 2017-04-11
@AlekseyPleshkov

It's called Trailing Closures.
Well, actually you did it. Then just call another function inside your closure.
An example with your implementation:

public func body(_ c:  () -> Void) {
    print(c)
}

body {
    body {
        // second lvl
    }
    text = "test"
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question