forked from lazerwalker/fuckingblocksyntax.com
-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.html
181 lines (161 loc) · 8.8 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
<!DOCTYPE html>
<html>
<head>
<title>How Do I Declare a Closure in Swift?</title>
<style>
body {
background-color: #292a35;
color: #fff;
font-family: Helvetica Neue, sans-serif;
font-size: 20px;
padding: 40px;
padding-bottom: 0;
}
strong {
color: #56bd4d;
}
h1 {
font-size: 40px;
margin-bottom: 1.5em;
text-align: center;
}
h2 {
font-weight: normal;
}
.return {
color: #ba246c;
}
.name {
color: #95cc6b;
}
.func {
color: #2dafc9;
}
.parameter-types, .parameters {
color: #2dafc9;
}
.closure-type {
color: #95cc6b;
}
.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;
}
.inline-code {
font-family: Menlo, monaco, monospace;
color: #ba246c;
}
footer {
font-size: 12px;
margin-top: 200px;
opacity: 0.4;
text-align: right;
}
a {
color: #0088D9;
text-decoration: none;
}
</style>
<style media='print'>
body {
background-color: #FFFFFF;
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="Closures" name="apple-mobile-web-app-title">
<base target="_top">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-51593041-1', 'fuckingclosuresyntax.com');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
</head>
<body>
<style>
#forkongithub a{background:#AA3333;color:#fff;text-decoration:none;font-family:Helvetica Neue,sans-serif;text-align:center;font-weight:bold;padding:5px 40px;font-size:1rem;line-height:2rem;position:relative;transition:0.5s;}#forkongithub a:hover{background:#c11;color:#fff;}#forkongithub a::before,#forkongithub a::after{content:"";width:100%;display:block;position:absolute;top:1px;left:0;height:1px;background:#fff;}#forkongithub a::after{bottom:1px;top:auto;}@media screen and (min-width:800px){#forkongithub{position:absolute;display:block;top:0;right:0;width:200px;overflow:hidden;height:200px;z-index:9999;}#forkongithub a{width:200px;position:absolute;top:60px;right:-60px;transform:rotate(45deg);-webkit-transform:rotate(45deg);-ms-transform:rotate(45deg);-moz-transform:rotate(45deg);-o-transform:rotate(45deg);box-shadow:4px 4px 10px rgba(0,0,0,0.3);}}
</style><span id="forkongithub"><a href="https://github.com/ZevEisenberg/fuckingclosuresyntax.com">Fork me on GitHub</a></span><br>
<br>
<h1>How Do I Declare a Closure in Swift?</h1>
<div>
<h2>As a <strong>variable</strong>:</h2><code><span class='return'>var</span> closureName: (<span class='parameter-types'>ParameterTypes</span>) -> <span class='return'>ReturnType</span> = { ... }</code>
</div>
<div>
<h2>As an <strong>optional variable</strong>:</h2><code><span class='return'>var</span> closureName: ((<span class='parameter-types'>ParameterTypes</span>) -> <span class='return'>ReturnType</span>)?</code>
</div>
<div>
<h2>As a <strong>type alias</strong>:</h2><code><span class='return'>typealias</span> <span class='closure-type'>ClosureType</span> = (<span class='parameter-types'>ParameterTypes</span>) -> <span class='return'>ReturnType</span></code>
</div>
<div>
<h2>As a <strong>constant</strong>:</h2><code><span class='return'>let</span> closureName: <span class='closure-type'>ClosureType</span> = { ... }</code>
</div>
<div>
<h2>As a <strong>parameter to another function</strong>:</h2><code><span class='name'>funcName</span>(parameter: (<span class='parameter-types'>ParameterTypes</span>) -> <span class='return'>ReturnType</span>)</code>
<p class='disclaimer'>Note: if the passed-in closure is going to outlive the scope of the method, e.g. if you are saving it to a property, it needs to be annotated with <span class='inline-code'>@escaping</span>.</p>
</div>
<div>
<h2>As an <strong>argument to a function call</strong>:</h2><code><span class='name'>funcName</span>({ (<span class='parameter-types'>ParameterTypes</span>) -> <span class='return'>ReturnType</span> in statements })</code>
</div>
<div>
<h2>As a <strong>function parameter</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span>(<span class='func'>by:</span> { (item1: <span class='func'>Int</span>, item2: <span class='func'>Int</span>) -> Bool <span class='return'>in return</span> item1 < item2 })</code>
<h2>As a <strong>function parameter with implied types</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span>(<span class='func'>by:</span> { (item1, item2) -> Bool <span class='return'>in return</span> item1 < item2 })</code>
<h2>As a <strong>function parameter with implied return type</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span>(<span class='func'>by:</span> { (item1, item2) <span class='return'>in return</span> item1 < item2 })</code>
<h2>As the <strong>last function parameter</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span> { (item1, item2) <span class='return'>in return</span> item1 < item2 }</code>
<h2>As the last parameter, <strong>using shorthand argument names</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span> { <span class='return'>return</span> $0 < $1 }</code>
<h2>As the last parameter, <strong>with an implied return value</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span> { $0 < $1 }</code>
<h2>As the last parameter, <strong>as a reference to an existing function</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span>(<span class='func'>by:</span> <)</code>
<h2>As a function parameter <strong>with explicit capture semantics</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span>(<span class='func'>by:</span> { [unowned self] (item1: <span class='func'>Int</span>, item2: <span class='func'>Int</span>) -> Bool <span class='return'>in return</span> item1 < item2 })</code>
<h2>As a function parameter <strong>with explicit capture semantics and inferred parameters / return type</strong>:</h2><code><span class='name'>array</span>.<span class='func'>sorted</span>(<span class='func'>by:</span> { [unowned self] <span class='return'>in return</span> $0 < $1 })</code>
</div>
<div class='disclaimer'>
This site is not intended to be an exhaustive list of all possible uses of closures.<br>
<br>
<br>
Unable to access this site due to the profanity in the URL? <strong><a href="http://goshdarnclosuresyntax.com">http://goshdarnclosuresyntax.com</a></strong> is a more work-friendly mirror.
</div>
<footer>
By <a href="http://zeveisenberg.com">Zev Eisenberg</a>, with apologies to <a href="http://lazerwalker.com/">Em Lazer-Walker</a>, who has a very bad memory for this sort of thing.
</footer>
</body>
</html>