joalbright / Inlinit

A new design pattern to allow closure initialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inlinit

A new design pattern to allow closure initialization.

Version

The master branch uses Swift 3.0.

Motivation

The desire is to initialize within a closure for unique customization while allowing the ability to use constants.

Example 1

Previous Code

// needs to be "var" to allow append below
var randomNumbersArray: [Int] = []

for _ in 0...10 {
        
    randomNumbersArray.append(Int(arc4random_uniform(100)))
    
}

New Code

// can now use let and append within initializer closure
let randomNumbersArray: [Int] {

	for _ in 0...10 {
	        
	    $0.append(Int(arc4random_uniform(100)))
	    
	}

}
Example 2
class Person: Inlinit {
        
    var age: Int = 0
    var name: String?
    
}

// initialize & set properties
var me = Person {
    
    $0.name = "Jo"
    $0.age = 32
    
}

// update properties (only works with classes)
me <- {
    
    $0.age = 30
    $0.name = "John"
    
}
Example 3
func newLabel(text: String) -> UILabel {

	let label: UILabel = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 40))
	
	label.text = text
	label.textColor = UIColor.magentaColor()
	
	return label

}

newLabel("This is lame...")

New Code

func newLabel(text: String) -> UILabel {

	return UILabel {

		$0.frame = CGRect(x: 10, y: 10, width: 100, height: 40)
		$0.textColor = UIColor.magentaColor()
	
	}.text = text

}

newLabel("This is Awesome!")

So many possibilities unlocked with this new design pattern

Author

Jo Albright

License

Inlinit is available under the MIT license. See the LICENSE file for more info.

About

A new design pattern to allow closure initialization

License:MIT License


Languages

Language:Swift 100.0%