Skip to content

Commit

Permalink
Merge pull request #12 from RougeWare/feature/9-Examples-In-Readme
Browse files Browse the repository at this point in the history
#9 Added examples to readme
  • Loading branch information
KyNorthstar authored Aug 31, 2019
2 parents b627819 + fc2cff7 commit 20ae92c
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 4 deletions.
183 changes: 183 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,186 @@ A few ways to have a lazily-initialized value in Swift 5.1. Note that, if you ar
The entire repository structure had to be changed in order to be compatible with Swift Package Manager ([#4](https://github.com/RougeWare/Swift-Lazy-Patterns/issues/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](https://github.com/RougeWare/Swift-Lazy-Patterns/issues/8)); it was almost entirely to service Swift Package manager.

In version 2.0.0, [this readme recommended](https://github.com/RougeWare/Swift-Lazy-Patterns/commit/68fd42023fe5642dd9841ea1411027f6cbc1032f#diff-04c6e90faac2675aa89e2176d2eec7d8) 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:

```swift

@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:

```plain
Hello, lazy!
Hello, lazy!
Overwritten
Overwritten
```

### More complex initializer ##

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

```swift

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):

```swift
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:

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


## `ResettableLazy` ##

The simple usage of this is very straightforward:

```swift

@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:

```plain
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:

```swift

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):

```swift
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:

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



## `FunctionalLazy` ##

This is functionally <sub>(ha!)</sub> the same as `Lazy`. The only difference is I thought it'd be fun to implement it with functions instead of enums. 🤓
64 changes: 60 additions & 4 deletions Tests/LazyContainersTests/LazyContainersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ import XCTest



var sideEffect: String?

func makeLazyA() -> String {
sideEffect = "Side effect A"
return "lAzy"
}



final class LazyContainersTests: XCTestCase {

@Lazy
var lazyInitWithPropertyWrapper = "lAzy"
@Lazy(initializer: makeLazyA)
var lazyInitWithPropertyWrapper: String

var lazyInitTraditionally = Lazy(wrappedValue: "lazy B")
var lazyInitTraditionally = Lazy<String>() {
sideEffect = "Side effect B"
return "lazy B"
}

@ResettableLazy
var resettableLazyInitWithPropertyWrapper = "lazy C"
Expand All @@ -31,7 +43,7 @@ final class LazyContainersTests: XCTestCase {


override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
sideEffect = nil
}


Expand All @@ -44,23 +56,39 @@ final class LazyContainersTests: XCTestCase {
// MARK: - `Lazy`

func testLazyInitWithPropertyWrapper() {
XCTAssertEqual(sideEffect, nil)
XCTAssertFalse(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, nil)
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")

lazyInitWithPropertyWrapper = "MAnual"

XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
}


Expand All @@ -72,6 +100,12 @@ final class LazyContainersTests: XCTestCase {
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("lazy B", lazyInitTraditionally.wrappedValue)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("lazy B", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("lazy B", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("lazy B", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)

lazyInitTraditionally.wrappedValue = "Manual B"

Expand All @@ -82,6 +116,28 @@ final class LazyContainersTests: XCTestCase {
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B", lazyInitTraditionally.wrappedValue)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)

lazyInitTraditionally.value = "Manual B2"

XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B2", lazyInitTraditionally.wrappedValue)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B2", lazyInitTraditionally.wrappedValue)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B2", lazyInitTraditionally.wrappedValue)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B2", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B2", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
XCTAssertEqual("Manual B2", lazyInitTraditionally.value)
XCTAssertTrue(lazyInitTraditionally.isInitialized)
}


Expand Down

0 comments on commit 20ae92c

Please sign in to comment.