Builder

protocol ThreeDimensions {
    var x: Double? {get}
    var y: Double? {get}
    var z: Double? {get}
}

class Point : ThreeDimensions {
    var x: Double?
    var y: Double?
    var z: Double?

    typealias PointBuilderClosure = (Point) -> ()

    init(buildClosure: PointBuilderClosure) {
        buildClosure(self)
    }
}

let fancyPoint = Point { point in
point.x = 0.1
point.y = 0.2
point.z = 0.3
}

fancyPoint.x
fancyPoint.y
fancyPoint.z
Shorter but oh-so-ugly alternative:

let uglierPoint = Point {
$0.x = 0.1
$0.y = 0.2
$0.z = 0.3
}

Tags: