Delay
Delay copied to clipboard
µframework for lazy evaluation in Swift.
Deprecated
This project is deprecated, and will not be updated in future. Memo is a complete replacement for its functionality, and is better-tested, -documented, and -spoken.
Delay
This is a Swift microframework which implements a lazily-evaluated memoizing wrapper type.
It’s a bit like a promise.
Why not just use a closure?
- Closures do not automatically memoize; Delay will evaluate zero or one times, whereas closures will evaluate every time they are called.
- Closures are currently subject to
non-fixed multi-payload enum layouterrors; since Delay is a class, it does not cause these problems forenumlayout.
Use
func expensiveComputation() -> Int
let delayedResult = delay(expensiveComputation())
let actualResult = delay.force() // explicitly force it
let otherActualResult: Int = delay // use the implicit __conversion
See Delay.swift for more details.
Integration
-
Add this repo as a submodule in e.g.
External/Delay:git submodule add https://github.com/robrix/Delay.git External/Delay -
Drag
Delay.xcodeprojinto your.xcworkspace/.xcodeproj. -
Add
Delay.frameworkto your target’sLink Binary With Librariesbuild phase. -
You may also want to add a
Copy Filesphase which copiesDelay.framework(and any other framework dependencies you need) into your bundle’sFrameworksdirectory. If your target is a framework, you may instead want the client app to includeDelay.framework.