forked from ZevEisenberg/fuckingclosuresyntax.com
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
243 lines (217 loc) · 10.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html>
<head>
<title>How Do I Write If Case Let in Swift?</title>
<style>
:root {
--background-color: #e9dcdc;
--text-color: #332443;
--inline-code-text-color: #128652;
--strong-text-color: #128652;
--let-text-color: #128652;
--expression-text-color:#2a524d;
--pattern-text-color: #2a524d;
--comment-text-color: #5d5959;
--code-block-color: #2a524d;
}
@media (prefers-color-scheme: dark) {
:root {
--background-color: #332443;
--text-color: #fff;
--inline-code-text-color: #fd5b58;
--strong-text-color: #fd5b58;
--let-text-color: #fd5b58;
--expression-text-color: #ebdddb;
--pattern-text-color: #ebdddb;
--comment-text-color: #9c8083;
--code-block-color: #ebdddb;
}
}
body {
background-color: var(--background-color);
color: var(--text-color);
font-family: Helvetica Neue, sans-serif;
font-size: 20px;
padding: 40px;
padding-bottom: 0;
}
strong {
color: var(--strong-text-color);
}
h1 {
font-size: 40px;
margin-bottom: 1.5em;
text-align: center;
}
h2 {
font-weight: normal;
}
.let {
color: var(--let-text-color);
}
.expression {
color: var(--expression-text-color);
}
.pattern {
color: var(--pattern-text-color);
}
.comment {
color: var(--comment-text-color);
}
.disclaimer {
line-height: 2em;
padding-bottom: 2em;
}
div p.disclaimer {
padding-left: 2em;
}
code {
display: block;
font-family: Menlo, monaco, monospace;
font-size: 20px;
margin-bottom: 3em;
margin-left: 40px;
margin-top: 1.5em;
color: var(--code-block-color);
}
.inline-code {
font-family: Menlo, monaco, monospace;
color: var(--inline-code-text-color);
}
footer {
font-size: 12px;
margin-top: 200px;
opacity: 0.4;
text-align: right;
}
a {
color: var(--let-text-color);
text-decoration: none;
}
</style>
<style media='print'>
body {
background-color: #fff;
font-size: 16px;
padding: 20px;
}
h1 {
font-size: 28px;
margin-bottom: 1em;
}
code {
font-size: 16px;
margin-bottom: 2em;
margin-left: 20px;
}
footer {
font-size: 10px;
margin-top: 40px;
}
</style>
<link href="apple-touch-icon-57x57.png" rel="apple-touch-icon" sizes="57x57">
<link href="apple-touch-icon-114x114.png" rel="apple-touch-icon" sizes="114x114">
<link href="apple-touch-icon-72x72.png" rel="apple-touch-icon" sizes="72x72">
<link href="apple-touch-icon-144x144.png" rel="apple-touch-icon" sizes="144x144">
<link href="apple-touch-icon-60x60.png" rel="apple-touch-icon" sizes="60x60">
<link href="apple-touch-icon-120x120.png" rel="apple-touch-icon" sizes="120x120">
<link href="apple-touch-icon-76x76.png" rel="apple-touch-icon" sizes="76x76">
<link href="apple-touch-icon-152x152.png" rel="apple-touch-icon" sizes="152x152">
<link href="favicon-196x196.png" rel="icon" sizes="196x196" type="image/png">
<link href="favicon-160x160.png" rel="icon" sizes="160x160" type="image/png">
<link href="favicon-96x96.png" rel="icon" sizes="96x96" type="image/png">
<link href="favicon-16x16.png" rel="icon" sizes="16x16" type="image/png">
<link href="favicon-32x32.png" rel="icon" sizes="32x32" type="image/png">
<meta content="Pattern matching syntax" name="apple-mobile-web-app-title">
<base target="_top">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-9504319-6"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-9504319-6');
</script>
</head>
<body>
<h1>How <i>Do</i> I Write If Case Let in Swift?</h1>
<div>
<h2><strong>if case let</strong> is an abbreviated form of <strong>switch</strong></h2>
<p class='disclaimer'><span class='inline-code'>case let</span> immediately precedes the <span class='pattern'>candidate pattern</span> in both versions. Confusingly though, when using <span class='inline-code'>if case let</span>, the <span class='expression'>value</span> comes <i>after</i> the = operator. So</p>
<code><span class='let'>if case let </span><span class='pattern'>Puppy.mastiff(droolRating, weight)</span> = <span class='expression'>fido</span> {<br/>
</code><p class='disclaimer'>is equivalent to: </p><code>
switch <span class='expression'>fido</span> {<br/>
<span class='let'>case let </span><span class='pattern'>Puppy.mastiff(droolRating, weight)</span>:<br/>
<br/>
<span class='comment'>// tell fido he's a good doggo</span><br/>
</code>
</div>
<div>
<h2><strong>let</strong> binds associated values</h2>
<p class='disclaimer'><span class='inline-code'>case let </span>is a shortcut that lets you bind all subsequent associated values with just variable names. But you can write each <span class='inline-code'>let</span> individually, just before the variable name:</p>
<code><span class='let'>if case </span><span class='pattern'>Puppy.mastiff(<span class='let'>let</span> droolRating, <span class='let'>let</span> weight)</span> = <span class='expression'>fido</span> {<br/>
<br/>
<span class='comment'>// does that pup need an extra treat?</span>
</code>
<p class='disclaimer'>Provide a single name to create a tuple of all associated values at once:</p>
<code><span class='let'>if case let</span> <span class='pattern'>Puppy.mastiff(characteristics)</span> = <span class='expression'>fido</span> {<br/>
<br/>
<span class='comment'>// how cute is this doggo, exactly?</span><br/>
</code>
<p class='disclaimer'>The <span class='let'>?</span> is syntactic sugar to only match non-nil optionals:</p>
<code><span class='let'>if case let</span> <span class='pattern'>Puppy.mastiff(droolFactor, weight)</span><span class = 'let'> ?</span> = <span class='expression'>potentialPup</span> {<br/>
<br/>
<span class='comment'>// oh thank goodness, a puppy</span><br/>
</code>
<p class='disclaimer'>Omit <span class='let'>let</span> entirely if you don't need the associated values:</p>
<code><span class='let'>if case </span><span class='pattern'>Puppy.mastiff</span> = <span class='expression'>fido</span> {<br/>
<br/>
<span class='comment'>// just hand me the pupster</span><br/>
</code>
</div>
<div>
<h2>Add conditions to <strong>if</strong> and <strong>guard</strong> using commas</h2>
<code><span class='let'>if case let</span> <span class='pattern'>Puppy.mastiff(droolRating, _)</span> = <span class='expression'>fido</span>, droolRating > 8 {<br/>
<br/>
<span class='comment'>// you're going to need a new couch</span><br/>
</code>
<code><span class='let'>guard case let </span><span class='pattern'>Puppy.mastiff(droolRating, _)</span> = <span class='expression'>fido</span>, droolRating < 10 else {<br/>
<br/>
<span class='comment'>// that dog is not coming in the house</span><br/>
</code>
</div>
<div>
<h2>Iterate using <strong>for</strong>, <strong>where</strong> and logical operators</h2>
<code><span class='let'>for case let </span><span class='pattern'>Puppy.mastiff(droolRating, weight)</span> in <span class='expression'>rescuePups</span> <span class='let'>where</span> droolRating > 5 && weight > 15.0 {<br/>
<br/>
<span class='comment'>// I'll take the chubby, soggy ones please</span><br/></code>
<p class='disclaimer'>Use <span class='let'>?</span> to iterate over just non-optional values, even when doubly nested in associated values:</p>
<code><span class='let'>for case let </span><span class='pattern'>SocialMedia.instagram(linkInBio</span><span class='let'>?</span>)<span class = 'let'>?</span> in <span class='expression'>pups.map(\.socialMedia)</span> {<br/>
<br/>
<span class='comment'>// all the links please</span><br/></code>
</div>
<div>
<h2>Bind non-optional derived values in chained expressions</h2>
<p class='disclaimer'>Really helpful if you need one non-optional assignment amidst a bunch of optional ones:</p>
<code>
if let rescueCenter = <span class='expression'>nearest()</span>, <span class='let'>case let</span> <span class='pattern'>puppies</span> = <span class='expression'>rescueCenter.puppies</span>, puppies.count < 10 {<br/>
<br/>
<span class='comment'>// better hurry or might not get enough</span><br/>
</code>
</div>
<p>Other more in-depth articles:
<li><a href='https://appventure.me/2015/08/20/swift-pattern-matching-in-detail/'>Swift Pattern Matching In Detail - Benedikt Terhechte</a></li>
<li><a href='https://oleb.net/blog/2015/09/swift-pattern-matching/'>Pattern Matching In Swift - Ole Begemann</a></li>
<li><a href='http://alisoftware.github.io/swift/pattern-matching/2016/05/16/pattern-matching-4/'>Pattern Matching - Crunchy Development</a></li>
<li><a href='https://www.bignerdranch.com/blog/pro-pattern-matching-in-swift/'>Pro Pattern-Matching in Swift - Nick Teissler, Big Nerd Ranch</a></li>
<li><a href='https://www.objc.io/blog/2018/09/04/switching-with-associated-values/'</a>Using the power of switching with associated values to write elegant, logical, meaningful code - objc.io</a></li> </p>
<br>
<br>
Unable to access this site due to the profanity in the URL? <strong><a href="http://goshdarnifcaseletsyntax.com">http://goshdarnifcaseletsyntax.com</a></strong> is a more work-friendly mirror.
<br>
</div>
<footer>
By <a href="http://twitter.com/zoejessica">Zoe Smith</a>, with many thanks to <a href="http://zeveisenberg.com">Zev Eisenberg</a> and <a href="http://lazerwalker.com/">Em Lazer-Walker</a>.
</footer>
</body>
</html>