Swift allows for lazy variables out-of-the-box, however they're fairly restricted.
- Lazy and Provider operations
- Short syntax
- Thread safe
let lazyInt = Lazy(1)
print(lazyInt.wasMade) // false
print(lazyInt.value) // 1
print(lazyInt.wasMade) // true
Support arithmetic operations:
let lazyInt = Lazy<Int>{ 1 + 2 * 5 } * 4 + 10
print(lazyInt.wasMade) // false
print(lazyInt.value) // 54
print(lazyInt.wasMade) // true
Short syntax:
let lazyInt = Lazy(1)
print(*lazyInt) // 1
let providerInt = Provider(1)
print(lazyInt.value) // 1
Support arithmetic operations:
let providerInt = Provider<Int>{ 1 + 2 * 5 } * 4 + 10
print(providerInt.value) // 54
Short syntax:
let providerInt = Provider(1)
print(*providerInt) // 1
var counter: Int = 0
let lazyInt = Lazy<Int> {
counter += 1
return counter
}
print(lazyInt.value) // 1
print(lazyInt.value) // 1
print(lazyInt.value) // 1
lazyInt.clear() // no provider
print(lazyInt.value) // 2
print(lazyInt.value) // 2
print(lazyInt.value) // 2
var counter: Int = 0
let providerInt = Provider<Int> {
counter += 1
return counter
}
print(providerInt.value) // 1
print(providerInt.value) // 2
print(providerInt.value) // 3
Add Provider with arguments:
let provider = Provider2<String, Int, Double> { "\($0) + \($1) = \($0 + $1)" }
print(provider.value(10, 20.0)) // "10 + 20.0 = 30.0"
Support 1, 2, 3, 4, 5 arguments count.
To install SwiftLazy with CocoaPods, add the following lines to your Podfile: pod 'SwiftLazy'
github "ivlevAstef/SwiftLazy"
iOS 11.0+,macOS 10.13+,tvOS 11.0+, watchOS 4.0+, Linux; ARC
Please raise a GitHub issue.
You can feel free to ask the question at e-mail: ivlev.stef@gmail.com.