Contents

shaneqi/cooking

Cook classes/structs with the taste you love.

Usage

Either class or struct has to conform to Edible protocol to have the wrapper with .cook.

Then extend Cooking with where constraint to extend your class or struct. You will have to use ingredient to refer the instance instead of using self.

For class, the constraint can use either == or :. For struct, the constraint has to use ==.

import Cooking

extension String: Edible {}
//  Use `==` for structs.
extension Cooking where Ingredient == String {

	static func bitterOne() -> String {
		return "😭😭😭"
	}

	func sweeten() -> String {
		return ingredient.replacingOccurrences(of: "😭", with: "πŸ˜‹")
	}
}

let bitterString = String.cook.bitterOne()
//  bitterString = "😭😭😭"
let sweetString  = bitterString.cook.sweeten()
//  sweetString  = "πŸ˜‹πŸ˜‹πŸ˜‹"

extension UIView: Edible {}
extension Cooking where Ingredient: UIView {
	func area() -> CGFloat {
		return ingredient.frame.width * ingredient.frame.height
	}
}

let view = UIView(frame: .init(origin: .zero, size: .init(width: 50, height: 20)))
let area = view.cook.area()
//  area = 1000

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To use Cooking in your Xcode project using CocoaPods, specify it in your Podfile:

pod 'Cooking'

Then, run the following command:

$ pod install

Swift Package Manager

Add this project as a dependency in your Package.swift file.

.Package(url: "https://github.com/ShaneQi/Cooking.git", majorVersion: 1, minor: 0)

Manually

Include file Sources/Cooking.swift in your project.

Limitation

  1. Can't extension structs with mutating func.
extension String: Edible {}

extension Cooking where Ingredient == String {

	mutating func addExclamation() {
		ingredient.append("!")
	}

}

"hello".cook.addExclamation()
// ERROR: ^ Cannot use mutating member, `cook` is a get-only property.
  1. Can't extension generics types.
extension Optional: Edible {}  
                               
extension Cooking where Ingredient == Optional {
//                                ERROR: ^ Reference to generic type 'Optional' requires arguments in <...>                          

	func someFunction() {}

}

Licensing

Apache-2.0

Package Metadata

Repository: shaneqi/cooking

Default branch: master

README: README.md