summaryrefslogtreecommitdiff
path: root/doc/articles/gos_declaration_syntax.html
blob: 455cced1d59b6f1ca4cd29257be1074aec1b5791 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!--{
"Title": "Go's Declaration Syntax"
}-->

<p>
Newcomers to Go wonder why the declaration syntax is different from the
tradition established in the C family. In this post we'll compare the
two approaches and explain why Go's declarations look as they do.
</p>

<p>
<b>C syntax</b>
</p>

<p>
First, let's talk about C syntax. C took an unusual and clever approach
to declaration syntax. Instead of describing the types with special
syntax, one writes an expression involving the item being declared, and
states what type that expression will have. Thus
</p>

<pre>
int x;
</pre>

<p>
declares x to be an int: the expression 'x' will have type int. In
general, to figure out how to write the type of a new variable, write an
expression involving that variable that evaluates to a basic type, then
put the basic type on the left and the expression on the right.
</p>

<p>
Thus, the declarations
</p>

<pre>
int *p;
int a[3];
</pre>

<p>
state that p is a pointer to int because '*p' has type int, and that a
is an array of ints because a[3] (ignoring the particular index value,
which is punned to be the size of the array) has type int.
</p>

<p>
What about functions? Originally, C's function declarations wrote the
types of the arguments outside the parens, like this:
</p>

<pre>
int main(argc, argv)
    int argc;
    char *argv[];
{ /* ... */ }
</pre>

<p>
Again, we see that main is a function because the expression main(argc,
argv) returns an int. In modern notation we'd write
</p>

<pre>
int main(int argc, char *argv[]) { /* ... */ }
</pre>

<p>
but the basic structure is the same.
</p>

<p>
This is a clever syntactic idea that works well for simple types but can
get confusing fast. The famous example is declaring a function pointer.
Follow the rules and you get this:
</p>

<pre>
int (*fp)(int a, int b);
</pre>

<p>
Here, fp is a pointer to a function because if you write the expression
(*fp)(a, b) you'll call a function that returns int. What if one of fp's
arguments is itself a function?
</p>

<pre>
int (*fp)(int (*ff)(int x, int y), int b)
</pre>

<p>
That's starting to get hard to read.
</p>

<p>
Of course, we can leave out the name of the parameters when we declare a
function, so main can be declared
</p>

<pre>
int main(int, char *[])
</pre>

<p>
Recall that argv is declared like this,
</p>

<pre>
char *argv[]
</pre>

<p>
so you drop the name from the <em>middle</em> of its declaration to construct
its type. It's not obvious, though, that you declare something of type
char *[] by putting its name in the middle.
</p>

<p>
And look what happens to fp's declaration if you don't name the
parameters:
</p>

<pre>
int (*fp)(int (*)(int, int), int)
</pre>

<p>
Not only is it not obvious where to put the name inside
</p>

<pre>
int (*)(int, int)
</pre>

<p>
it's not exactly clear that it's a function pointer declaration at all.
And what if the return type is a function pointer?
</p>

<pre>
int (*(*fp)(int (*)(int, int), int))(int, int)
</pre>

<p>
It's hard even to see that this declaration is about fp.
</p>

<p>
You can construct more elaborate examples but these should illustrate
some of the difficulties that C's declaration syntax can introduce.
</p>

<p>
There's one more point that needs to be made, though. Because type and
declaration syntax are the same, it can be difficult to parse
expressions with types in the middle. This is why, for instance, C casts
always parenthesize the type, as in
</p>

<pre>
(int)M_PI
</pre>

<p>
<b>Go syntax</b>
</p>

<p>
Languages outside the C family usually use a distinct type syntax in
declarations. Although it's a separate point, the name usually comes
first, often followed by a colon. Thus our examples above become
something like (in a fictional but illustrative language)
</p>

<pre>
x: int
p: pointer to int
a: array[3] of int
</pre>

<p>
These declarations are clear, if verbose - you just read them left to
right. Go takes its cue from here, but in the interests of brevity it
drops the colon and removes some of the keywords:
</p>

<pre>
x int
p *int
a [3]int
</pre>

<p>
There is no direct correspondence between the look of [3]int and how to
use a in an expression. (We'll come back to pointers in the next
section.) You gain clarity at the cost of a separate syntax.
</p>

<p>
Now consider functions. Let's transcribe the declaration for main, even
though the main function in Go takes no arguments:
</p>

<pre>
func main(argc int, argv *[]byte) int
</pre>

<p>
Superficially that's not much different from C, but it reads well from
left to right:
</p>

<p>
<em>function main takes an int and a pointer to a slice of bytes and returns an int.</em>
</p>

<p>
Drop the parameter names and it's just as clear - they're always first
so there's no confusion.
</p>

<pre>
func main(int, *[]byte) int
</pre>

<p>
One value of this left-to-right style is how well it works as the types
become more complex. Here's a declaration of a function variable
(analogous to a function pointer in C):
</p>

<pre>
f func(func(int,int) int, int) int
</pre>

<p>
Or if f returns a function:
</p>

<pre>
f func(func(int,int) int, int) func(int, int) int
</pre>

<p>
It still reads clearly, from left to right, and it's always obvious
which name is being declared - the name comes first.
</p>

<p>
The distinction between type and expression syntax makes it easy to
write and invoke closures in Go:
</p>

<pre>
sum := func(a, b int) int { return a+b } (3, 4)
</pre>

<p>
<b>Pointers</b>
</p>

<p>
Pointers are the exception that proves the rule. Notice that in arrays
and slices, for instance, Go's type syntax puts the brackets on the left
of the type but the expression syntax puts them on the right of the
expression:
</p>

<pre>
var a []int
x = a[1]
</pre>

<p>
For familiarity, Go's pointers use the * notation from C, but we could
not bring ourselves to make a similar reversal for pointer types. Thus
pointers work like this
</p>

<pre>
var p *int
x = *p
</pre>

<p>
We couldn't say
</p>

<pre>
var p *int
x = p*
</pre>

<p>
because that postfix * would conflate with multiplication. We could have
used the Pascal ^, for example:
</p>

<pre>
var p ^int
x = p^
</pre>

<p>
and perhaps we should have (and chosen another operator for xor),
because the prefix asterisk on both types and expressions complicates
things in a number of ways. For instance, although one can write
</p>

<pre>
[]int("hi")
</pre>

<p>
as a conversion, one must parenthesize the type if it starts with a *:
</p>

<pre>
(*int)(nil)
</pre>

<p>
Had we been willing to give up * as pointer syntax, those parentheses
would be unnecessary.
</p>

<p>
So Go's pointer syntax is tied to the familiar C form, but those ties
mean that we cannot break completely from using parentheses to
disambiguate types and expressions in the grammar.
</p>

<p>
Overall, though, we believe Go's type syntax is easier to understand
than C's, especially when things get complicated.
</p>

<p>
<b>Notes</b>
</p>

<p>
Go's declarations read left to right. It's been pointed out that C's
read in a spiral! See <a href="http://c-faq.com/decl/spiral.anderson.html">
The "Clockwise/Spiral Rule"</a> by David Anderson.
</p>