Swiftpack.co - Package - hb1love/Scope

Scope

Build Status Platform Swift CocoaPods Carthage compatible

🌷 Scoping Functions of Kotlin Style for Readable Code

Usage

also, with, let, run, apply

Initialize Instance with also in closure

let org = Organazation().also {
    $0.name = "podo"
    $0.member = Member(name: "hb1love", role: .owner)
}

Similarly, we can be used like this. with

with(organization.member) {
    $0.name = "podo"
    $0.role = .owner
}

Imagine if organization.member could be null, maybe it looks like this: Use let !

// so bad..
with(organization.member) {
    $0?.name = "hb1love"
    $0?.role = .owner
}

// Good!
org.member?.let {
    $0.name = "hb1love"
    $0.role = .owner
}

it is fully enclosed within the run scope.

var age = 17
run {
    var age = 27
    print(age) // 27
}
print(age) // 17
let org = run { () -> Organazation in
    someFunc1()
    someFunc2()
    someFunc3()
    return Organazation(name: "podo")
}

Use apply, when you need any logic after initialization

let org = Organazation().apply {
    someFunc1()
    someFunc2()
}

apply has no parameters. It just returns an instance.

Requirements

  • iOS 8.0+
  • Xcode 11.3.1+
  • Swift 4.0+

Installation

pod 'Scope'
github "hb1love/Scope"

License

Scope is available under the MIT license. See the LICENSE for details.

Github

link
Stars: 3

Dependencies

Used By

Total: 0