SwiftLazy

1.2.0

Classes (Lazy, Provider) is intended for late initialization.
ivlevAstef/SwiftLazy

What's New

Remove SpinLock

2023-07-22T16:46:42Z

Remove SpinLock and increase minimum OS version

CocoaPods Version Carthage compatible License Swift Version Platform

SwiftLazy

Swift allows for lazy variables out-of-the-box, however they're fairly restricted.

Features

  • Lazy and Provider operations
  • Short syntax
  • Thread safe

Usage

Lazy

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

Provider

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

Difference Lazy vs Provider

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

Version 1.1.0

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.

Install

Via CocoaPods.

To install SwiftLazy with CocoaPods, add the following lines to your Podfile: pod 'SwiftLazy'

Via Carthage.

github "ivlevAstef/SwiftLazy"

The library is integrated with DITranquillity

Requirements

iOS 11.0+,macOS 10.13+,tvOS 11.0+, watchOS 4.0+, Linux; ARC

Feedback

I've found a bug, or have a feature request

Please raise a GitHub issue.

Question?

You can feel free to ask the question at e-mail: ivlev.stef@gmail.com.

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Nov 13 2024 23:17:50 GMT-1000 (Hawaii-Aleutian Standard Time)