Skip to content
A few lazy-initialization patterns for Swift 5.1
Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
.swiftpm/xcode/package.xcworkspace
Sources/LazyContainers
Tests
.gitignore
LICENSE.txt
Package.swift
README.md

README.md

CodeFactor Swift GitHub workflow

Swift Lazy Containers

A few ways to have a lazily-initialized value in Swift 5.1. Note that, if you are OK with the behavior of Swift's lazy keyword, you should use that. This is for those who want very specific behaviors:

  • Lazy: A non-resettable lazy pattern, to guarantee lazy behavior across Swift language versions
  • ResettableLazy: A resettable lazy pattern, whose value is generated and cached only when first needed, and can be destroyed when no longer needed.
  • FunctionalLazy: An idea about how to approach the lazy pattern by using functions instead of branches.

Compatibility Notice

The entire repository structure had to be changed in order to be compatible with Swift Package Manager (#4). Because of this, the API version changed from 2.0.0 to 3.0.0. Very little of the actual API changed along with this (#8); it was almost entirely to service Swift Package manager.

In version 2.0.0, this readme recommended that you change any reference to ./Lazy.swift to ./LazyContainers/Sources/LazyContainers/LazyContainers.swift. Unfortunately, that wasn't compatible with Swift Package Manager, so ./Lazy.swift was changed to ./Sources/LazyContainers/LazyContainers.swift. Because of this, please change any reference to ./LazyContainers/Sources/LazyContainers/LazyContainers.swift to ./Sources/LazyContainers/LazyContainers.swift. Sorry about that 🤷🏽‍

Examples

It's easy to use each of these. Simply place the appropriate one as a property wrapper where you want it.

Lazy

The simple usage of this is very straightforward:

@Lazy
var myLazyString = "Hello, lazy!"

print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

myLazyString = "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"

This will print:

Hello, lazy!
Hello, lazy!
Overwritten
Overwritten

More complex initializer

If you have complex initializer logic, you can pass that to the property wrapper:

func makeLazyString() -> String {
    print("Initializer side-effect")
    return "Hello, lazy!"
}

@Lazy(initializer: makeLazyString)
var myLazyString: String

print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

myLazyString = "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"

You can also use it directly (instaed of as a property wrapper):

var myLazyString = Lazy<String>() {
    print("Initializer side-effect")
    return "Hello, lazy!"
}

print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"

myLazyString.wrappedValue = "Overwritten"
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"

These will both print:

Initializer side-effect
Hello, lazy!
Hello, lazy!
Overwritten
Overwritten

ResettableLazy

The simple usage of this is very straightforward:

@ResettableLazy
var myLazyString = "Hello, lazy!"

print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

_myLazyString.clear()
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

myLazyString = "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"
_myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value  "Hello, lazy!"

This will print:

Hello, lazy!
Hello, lazy!
Hello, lazy!
Hello, lazy!
Overwritten
Hello, lazy!

More complex initializer

If you have complex initializer logic, you can pass that to the property wrapper:

func makeLazyString() -> String {
    print("Initializer side-effect")
    return "Hello, lazy!"
}

@ResettableLazy(initializer: makeLazyString)
var myLazyString: String

print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

_myLazyString.clear()
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString) // Just returns the value "Hello, lazy!"

myLazyString = "Overwritten"
print(myLazyString) // Just returns the value "Overwritten"
_myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value  "Hello, lazy!"

You can also use it directly (instaed of as a property wrapper):

var myLazyString = ResettableLazy<String>() {
    print("Initializer side-effect")
    return "Hello, lazy!"
}

print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"

myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"

myLazyString.wrappedValue = "Overwritten"
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"
_myLazyString.clear()
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value  "Hello, lazy!"

These will both print:

Initializer side-effect
Hello, lazy!
Hello, lazy!
Initializer side-effect
Hello, lazy!
Hello, lazy!
Overwritten
Initializer side-effect
Hello, lazy!

FunctionalLazy

This is functionally (ha!) the same as Lazy. The only difference is I thought it'd be fun to implement it with functions instead of enums. 🤓

You can’t perform that action at this time.