playground.swift
@resultBuilder
public struct AdderBuilder {
//supports DSL in block
public static func buildBlock(_ components: Int...) -> Int {
components.reduce(0, +)
}
//supports "if" statement without "else"
public static func buildOptional(_ component: Int?) -> Int{
return component ?? 0
}
//supports "if else" statement
public static func buildEither(first component: Int) -> Int {
return component
}
public static func buildEither(second component : Int) -> Int{
return component
}
//supports "for in" statement
public static func buildArray(_ components: [Int]) -> Int{
return components.reduce(0, +)
}
}
func blockAdder(@AdderBuilder block:()->Int)->Int{
return block()
}
let sum = blockAdder{//buildBlock()
0
1
if 0<1 {//buildOptional()
2
}
if 0<1{//buildEither(first:)
3
}else{//buildEither(first:)
4
}
for _ in 0...2 {//buildArray
5
}
}