You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+183Lines changed: 183 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,3 +14,186 @@ A few ways to have a lazily-initialized value in Swift 5.1. Note that, if you ar
14
14
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.
15
15
16
16
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 🤷🏽
17
+
18
+
19
+
20
+
# Examples #
21
+
22
+
It's easy to use each of these. Simply place the appropriate one as a property wrapper where you want it.
23
+
24
+
25
+
## `Lazy` ##
26
+
27
+
The simple usage of this is very straightforward:
28
+
29
+
```swift
30
+
31
+
@Lazy
32
+
var myLazyString ="Hello, lazy!"
33
+
34
+
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
35
+
print(myLazyString) // Just returns the value "Hello, lazy!"
36
+
37
+
myLazyString ="Overwritten"
38
+
print(myLazyString) // Just returns the value "Overwritten"
39
+
print(myLazyString) // Just returns the value "Overwritten"
40
+
```
41
+
42
+
This will print:
43
+
44
+
```plain
45
+
Hello, lazy!
46
+
Hello, lazy!
47
+
Overwritten
48
+
Overwritten
49
+
```
50
+
51
+
### More complex initializer ##
52
+
53
+
If you have complex initializer logic, you can pass that to the property wrapper:
54
+
55
+
```swift
56
+
57
+
funcmakeLazyString() ->String {
58
+
print("Initializer side-effect")
59
+
return"Hello, lazy!"
60
+
}
61
+
62
+
@Lazy(initializer: makeLazyString)
63
+
var myLazyString: String
64
+
65
+
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
66
+
print(myLazyString) // Just returns the value "Hello, lazy!"
67
+
68
+
myLazyString ="Overwritten"
69
+
print(myLazyString) // Just returns the value "Overwritten"
70
+
print(myLazyString) // Just returns the value "Overwritten"
71
+
```
72
+
73
+
You can also use it directly (instaed of as a property wrapper):
74
+
75
+
```swift
76
+
var myLazyString = Lazy<String>() {
77
+
print("Initializer side-effect")
78
+
return"Hello, lazy!"
79
+
}
80
+
81
+
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
82
+
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"
83
+
84
+
myLazyString.wrappedValue="Overwritten"
85
+
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"
86
+
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"
87
+
```
88
+
89
+
These will both print:
90
+
91
+
```plain
92
+
Initializer side-effect
93
+
Hello, lazy!
94
+
Hello, lazy!
95
+
Overwritten
96
+
Overwritten
97
+
```
98
+
99
+
100
+
## `ResettableLazy` ##
101
+
102
+
The simple usage of this is very straightforward:
103
+
104
+
```swift
105
+
106
+
@ResettableLazy
107
+
var myLazyString ="Hello, lazy!"
108
+
109
+
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
110
+
print(myLazyString) // Just returns the value "Hello, lazy!"
111
+
112
+
_myLazyString.clear()
113
+
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
114
+
print(myLazyString) // Just returns the value "Hello, lazy!"
115
+
116
+
myLazyString ="Overwritten"
117
+
print(myLazyString) // Just returns the value "Overwritten"
118
+
_myLazyString.clear()
119
+
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
120
+
```
121
+
122
+
This will print:
123
+
124
+
```plain
125
+
Hello, lazy!
126
+
Hello, lazy!
127
+
Hello, lazy!
128
+
Hello, lazy!
129
+
Overwritten
130
+
Hello, lazy!
131
+
```
132
+
133
+
### More complex initializer ##
134
+
135
+
If you have complex initializer logic, you can pass that to the property wrapper:
136
+
137
+
```swift
138
+
139
+
funcmakeLazyString() ->String {
140
+
print("Initializer side-effect")
141
+
return"Hello, lazy!"
142
+
}
143
+
144
+
@ResettableLazy(initializer: makeLazyString)
145
+
var myLazyString: String
146
+
147
+
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
148
+
print(myLazyString) // Just returns the value "Hello, lazy!"
149
+
150
+
_myLazyString.clear()
151
+
print(myLazyString) // Initializes, caches, and returns the value "Hello, lazy!"
152
+
print(myLazyString) // Just returns the value "Hello, lazy!"
153
+
154
+
myLazyString ="Overwritten"
155
+
print(myLazyString) // Just returns the value "Overwritten"
156
+
_myLazyString.clear()
157
+
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
158
+
```
159
+
160
+
You can also use it directly (instaed of as a property wrapper):
161
+
162
+
```swift
163
+
var myLazyString = ResettableLazy<String>() {
164
+
print("Initializer side-effect")
165
+
return"Hello, lazy!"
166
+
}
167
+
168
+
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
169
+
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"
170
+
171
+
myLazyString.clear()
172
+
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
173
+
print(myLazyString.wrappedValue) // Just returns the value "Hello, lazy!"
174
+
175
+
myLazyString.wrappedValue="Overwritten"
176
+
print(myLazyString.wrappedValue) // Just returns the value "Overwritten"
177
+
_myLazyString.clear()
178
+
print(myLazyString.wrappedValue) // Initializes, caches, and returns the value "Hello, lazy!"
179
+
```
180
+
181
+
These will both print:
182
+
183
+
```plain
184
+
Initializer side-effect
185
+
Hello, lazy!
186
+
Hello, lazy!
187
+
Initializer side-effect
188
+
Hello, lazy!
189
+
Hello, lazy!
190
+
Overwritten
191
+
Initializer side-effect
192
+
Hello, lazy!
193
+
```
194
+
195
+
196
+
197
+
## `FunctionalLazy` ##
198
+
199
+
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. 🤓
0 commit comments