-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypes.html
More file actions
927 lines (824 loc) · 57.7 KB
/
Types.html
File metadata and controls
927 lines (824 loc) · 57.7 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Types</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter ">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name">Language Guide
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="The Basics.html#TP40014097-CH5-XID_399" data-id="TP40014097-CH5-XID_399" class="">The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368" class="">Strings and Characters</a>
</li>
<li class="nav-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" class="">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204" class="">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185" class="">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323" class="">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300" class="">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393" class="">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251" class="">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266" class="">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182" class="">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50" class="">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312" class="">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443" class="">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309" class="">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191" class="">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345" class="">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234" class="">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28" class="">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name nav-part-active open-part">Language Reference
<ul class="nav-chapters" style="height: 299px;">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794" class="">Lexical Structure</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Types.html" data-id="TP40014097-CH31-XID_988" class="nav-chapter-active">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html#TP40014097-CH36-XID_878" data-id="TP40014097-CH36-XID_878">Patterns</a>
</li>
<li class="nav-chapter">
<a href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774" data-id="TP40014097-CH37-XID_774">Generic Parameters and Arguments</a>
</li>
<li class="nav-chapter">
<a href="Summary of the Grammar.html#TP40014097-CH38-XID_1030" data-id="TP40014097-CH38-XID_1030">Summary of the Grammar</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="chapter">
<a name="TP40014097-CH31"></a><a name="TP40014097-CH31-XID_988"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Types</h2>
<section id="mini_toc" class="hideInXcode" role="navigation">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_991">
Type Annotation
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_994">
Type Identifier
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_998">
Tuple Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1005">
Function Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1008">
Array Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1011">
Optional Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1014">
Implicitly Unwrapped Optional Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1017">
Protocol Composition Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1022">
Metatype Type
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1025">
Type Inheritance Clause
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH31-XID_1029">
Type Inference
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
In Swift, there are two kinds of types: named types and compound types. A <em>named type</em> is a type that can be given a particular name when it is defined. Named types include classes, structures, enumerations, and protocols. For example, instances of a user-defined class named <code class="code-voice">MyClass</code> have the type <code class="code-voice">MyClass</code>. In addition to user-defined named types, the Swift standard library defines many commonly used named types, including those that represent arrays, dictionaries, and optional values.
</p>
<p class="para">
Data types that are normally considered basic or primitive in other languages—such as types that represent numbers, characters, and strings—are actually named types, defined and implemented in the Swift standard library using structures. Because they are named types, you can extend their behavior to suit the needs of your program, using an extension declaration, discussed in <span class="x-name"><a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a></span> and <span class="x-name"><a href="Declarations.html#TP40014097-CH34-XID_633" data-id="TP40014097-CH34-XID_633">Extension Declaration</a></span>.
</p>
<p class="para">
A <em>compound type</em> is a type without a name, defined in the Swift language itself. There are two compound types: function types and tuple types. A compound type may contain named types and other compound types. For instance, the tuple type <code class="code-voice">(Int, (Int, Int))</code> contains two elements: The first is the named type <code class="code-voice">Int</code>, and the second is another compound type <code class="code-voice">(Int, Int)</code>.
</p>
<p class="para">
This chapter discusses the types defined in the Swift language itself and describes the type inference behavior of Swift.
</p>
<div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/type"></a>
<span class="syntax-def-name">
type
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-type" data-id="//apple_ref/swift/grammar/array-type">array-type</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/function-type" data-id="//apple_ref/swift/grammar/function-type">function-type</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-identifier" data-id="//apple_ref/swift/grammar/type-identifier">type-identifier</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/tuple-type" data-id="//apple_ref/swift/grammar/tuple-type">tuple-type</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/optional-type" data-id="//apple_ref/swift/grammar/optional-type">optional-type</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/implicitly-unwrapped-optional-type" data-id="//apple_ref/swift/grammar/implicitly-unwrapped-optional-type">implicitly-unwrapped-optional-type</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/protocol-composition-type" data-id="//apple_ref/swift/grammar/protocol-composition-type">protocol-composition-type</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/metatype-type" data-id="//apple_ref/swift/grammar/metatype-type">metatype-type</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_991"></a>
<h3 class="section-name" tabindex="0">Type Annotation</h3>
<p class="para">
A <em>type annotation</em> explicitly specifies the type of a variable or expression. Type annotations begin with a colon (<code class="code-voice">:</code>) and end with a type, as the following examples show:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">someTuple</span>: (<span class="n"></span>) = (<span class="m">3.14159</span>, <span class="m">2.71828</span>)</code></li>
<li><code class="code-voice"><span class="kt">func</span> <span class="vc">someFunction</span>(<span class="vc">a</span>: <span class="n"></span>) { <span class="c">/* ... */</span> }</code></li>
</ul>
</div>
</section><p class="para">
In the first example, the expression <code class="code-voice">someTuple</code> is specified to have the tuple type <code class="code-voice">(Double, Double)</code>. In the second example, the parameter <code class="code-voice">a</code> to the function <code class="code-voice">someFunction</code> is specified to have the type <code class="code-voice">Int</code>.
</p><p class="para">
Type annotations can contain an optional list of type attributes before the type.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type annotation
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-annotation"></a>
<span class="syntax-def-name">
type-annotation
</span>
<span class="arrow">
→
</span><code class="literal">:</code><span class="optional"><span class="syntactic-cat"><a href="Attributes.html#//apple_ref/swift/grammar/attributes" data-id="//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a href="Types.html#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_994"></a>
<h3 class="section-name" tabindex="0">Type Identifier</h3>
<p class="para">
A type identifier refers to either a named type or a type alias of a named or compound type.
</p><p class="para">
Most of the time, a type identifier directly refers to a named type with the same name as the identifier. For example, <code class="code-voice">Int</code> is a type identifier that directly refers to the named type <code class="code-voice">Int</code>, and the type identifier <code class="code-voice">Dictionary<String, Int></code> directly refers to the named type <code class="code-voice">Dictionary<String, Int></code>.
</p><p class="para">
There are two cases in which a type identifier does not refer to a type with the same name. In the first case, a type identifier refers to a type alias of a named or compound type. For instance, in the example below, the use of <code class="code-voice">Point</code> in the type annotation refers to the tuple type <code class="code-voice">(Int, Int)</code>.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">typealias</span> <span class="vc">Point</span> = (<span class="n"></span>)</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">origin</span>: <span class="n"></span> = (<span class="m">0</span>, <span class="m">0</span>)</code></li>
</ul>
</div>
</section><p class="para">
In the second case, a type identifier uses dot (<code class="code-voice">.</code>) syntax to refer to named types declared in other modules or nested within other types. For example, the type identifier in the following code references the named type <code class="code-voice">MyType</code> that is declared in the <code class="code-voice">ExampleModule</code> module.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">someValue</span>: <span class="n"></span></code></li>
</ul>
</div>
</section><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type identifier
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-identifier"></a>
<span class="syntax-def-name">
type-identifier
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-name" data-id="//apple_ref/swift/grammar/type-name">type-name</a></span><span class="optional"><span class="syntactic-cat"><a href="Generic Parameters and Arguments.html#//apple_ref/swift/grammar/generic-argument-clause" data-id="//apple_ref/swift/grammar/generic-argument-clause">generic-argument-clause</a></span><sub class="subscript">opt</sub></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-name" data-id="//apple_ref/swift/grammar/type-name">type-name</a></span><span class="optional"><span class="syntactic-cat"><a href="Generic Parameters and Arguments.html#//apple_ref/swift/grammar/generic-argument-clause" data-id="//apple_ref/swift/grammar/generic-argument-clause">generic-argument-clause</a></span><sub class="subscript">opt</sub></span><code class="literal">.</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-identifier" data-id="//apple_ref/swift/grammar/type-identifier">type-identifier</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-name"></a>
<span class="syntax-def-name">
type-name
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Lexical Structure.html#//apple_ref/swift/grammar/identifier" data-id="//apple_ref/swift/grammar/identifier">identifier</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_998"></a>
<h3 class="section-name" tabindex="0">Tuple Type</h3>
<p class="para">
A tuple type is a comma-separated list of zero or more types, enclosed in parentheses.
</p><p class="para">
You can use a tuple type as the return type of a function to enable the function to return a single tuple containing multiple values. You can also name the elements of a tuple type and use those names to refer to the values of the individual elements. An element name consists of an identifier followed immediately by a colon (:). For an example that demonstrates both of these features, see <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_212" data-id="TP40014097-CH10-XID_212">Functions with Multiple Return Values</a></span>.
</p><p class="para">
<code class="code-voice">Void</code> is a typealias for the the empty tuple type, <code class="code-voice">()</code>. If there is only one element inside the parentheses, the type is simply the type of that element. For example, the type of <code class="code-voice">(Int)</code> is <code class="code-voice">Int</code>, not <code class="code-voice">(Int)</code>. As a result, you can label a tuple element only when the tuple type has two or more elements.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a tuple type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-type"></a>
<span class="syntax-def-name">
tuple-type
</span>
<span class="arrow">
→
</span><code class="literal">(</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/tuple-type-body" data-id="//apple_ref/swift/grammar/tuple-type-body">tuple-type-body</a></span><sub class="subscript">opt</sub></span><code class="literal">)</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-type-body"></a>
<span class="syntax-def-name">
tuple-type-body
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/tuple-type-element-list" data-id="//apple_ref/swift/grammar/tuple-type-element-list">tuple-type-element-list</a></span><span class="optional"><code class="literal">...</code><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-type-element-list"></a>
<span class="syntax-def-name">
tuple-type-element-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/tuple-type-element" data-id="//apple_ref/swift/grammar/tuple-type-element">tuple-type-element</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/tuple-type-element" data-id="//apple_ref/swift/grammar/tuple-type-element">tuple-type-element</a></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/tuple-type-element-list" data-id="//apple_ref/swift/grammar/tuple-type-element-list">tuple-type-element-list</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-type-element"></a>
<span class="syntax-def-name">
tuple-type-element
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="optional"><span class="syntactic-cat"><a href="Attributes.html#//apple_ref/swift/grammar/attributes" data-id="//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><span class="optional"><code class="literal">inout</code><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span>
</span><span class="alternative">
<span class="optional"><code class="literal">inout</code><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/element-name" data-id="//apple_ref/swift/grammar/element-name">element-name</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-annotation" data-id="//apple_ref/swift/grammar/type-annotation">type-annotation</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/element-name"></a>
<span class="syntax-def-name">
element-name
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Lexical Structure.html#//apple_ref/swift/grammar/identifier" data-id="//apple_ref/swift/grammar/identifier">identifier</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1005"></a>
<h3 class="section-name" tabindex="0">Function Type</h3>
<p class="para">
A function type represents the type of a function, method, or closure and consists of a parameter and return type separated by an arrow (<code class="code-voice">-></code>):
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><em class="variable-text">parameter type</em> -> <em class="variable-text">return type</em></pre></li>
</ul>
</div><p class="para">
Because the <em>parameter type</em> and the <em>return type</em> can be a tuple type, function types support functions and methods that take multiple paramaters and return multiple values.
</p><p class="para">
You can apply the <code class="code-voice">auto_closure</code> attribute to a function type that has a parameter type of <code class="code-voice">()</code> and that returns the type of an expression (see <span class="x-name"><a href="Attributes.html#TP40014097-CH35-XID_463" data-id="TP40014097-CH35-XID_463">Type Attributes</a></span>). An autoclosure function captures an implicit closure over the specified expression, instead of the expression itself. The following example uses the <code class="code-voice">auto_closure</code> attribute in defining a very simple assert function:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">func</span> <span class="vc">simpleAssert</span>(<span class="vc">condition</span>: <span class="kt">@auto_closure</span> () -> <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">if</span> !<span class="vc">condition</span>() {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="vc">message</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">testNumber</span> = <span class="m">5</span></code></li>
<li><code class="code-voice"><span class="vc">simpleAssert</span>(<span class="vc">testNumber</span> % <span class="m">2</span> == <span class="m">0</span>, <span class="s">"testNumber isn't an even number."</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "testNumber isn't an even number."</span></code></li>
</ul>
</div>
</section><p class="para">
A function type can have a variadic parameter as the last parameter in its <em>parameter type</em>. Syntactically, a variadic parameter consists of a base type name followed immediately by three dots (<code class="code-voice">...</code>), as in <code class="code-voice">Int...</code>. A variadic parameter is treated as an array that contains elements of the base type name. For instance, the variadic parameter <code class="code-voice">Int...</code> is treated as <code class="code-voice">Int[]</code>. For an example that uses a variadic parameter, see <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_222" data-id="TP40014097-CH10-XID_222">Variadic Parameters</a></span>.
</p><p class="para">
To specify an in-out parameter, prefix the parameter type with the <code class="code-voice">inout</code> keyword. You can’t mark a variadic parameter or a return type with the <code class="code-voice">inout</code> keyword. In-out parameters are discussed in <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_226" data-id="TP40014097-CH10-XID_226">In-Out Parameters</a></span>.
</p><p class="para">
The type of a curried function is equivalent to a nested function type. For example, the type of the curried function <code class="code-voice">addTwoNumbers()()</code> below is <code class="code-voice">Int -> Int -> Int</code>:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">func</span> <span class="vc">addTwoNumbers</span>(<span class="vc">a</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">a</span> + <span class="vc">b</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="vc">addTwoNumbers</span>(<span class="m">4</span>)(<span class="m">5</span>) <span class="c">// Returns 9</span></code></li>
</ul>
</div>
</section><p class="para">
The function types of a curried function are grouped from right to left. For instance, the function type <code class="code-voice">Int -> Int -> Int</code> is understood as <code class="code-voice">Int -> (Int -> Int)</code>—that is, a function that takes an <code class="code-voice">Int</code> and returns another function that takes and return an <code class="code-voice">Int</code>. For example, you can rewrite the curried function <code class="code-voice">addTwoNumbers()()</code> as the following nested function:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">func</span> <span class="vc">addTwoNumbers</span>(<span class="vc">a</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">func</span> <span class="vc">addTheSecondNumber</span>(<span class="vc">b</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">a</span> + <span class="vc">b</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">addTheSecondNumber</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="vc">addTwoNumbers</span>(<span class="m">4</span>)(<span class="m">5</span>) <span class="c">// Returns 9</span></code></li>
</ul>
</div>
</section><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a function type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/function-type"></a>
<span class="syntax-def-name">
function-type
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span><code class="literal">-></code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1008"></a>
<h3 class="section-name" tabindex="0">Array Type</h3>
<p class="para">
The Swift language uses square brackets (<code class="code-voice">[]</code>) immediately after the name of a type as syntactic sugar for the named type <code class="code-voice">Array<T></code>, which is defined in the Swift standard library. In other words, the following two declarations are equivalent:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">someArray</span>: <span class="n"></span>[] = [<span class="s">"Alex"</span>, <span class="s">"Brian"</span>, <span class="s">"Dave"</span>]</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">someArray</span>: <span class="n"></span>> = [<span class="s">"Alex"</span>, <span class="s">"Brian"</span>, <span class="s">"Dave"</span>]</code></li>
</ul>
</div>
</section><p class="para">
In both cases, the constant <code class="code-voice">someArray</code> is declared as an array of strings. The elements of an array can be accessed using square brackets as well: <code class="code-voice">someArray[0]</code> refers to the element at index 0, <code class="code-voice">"Alex"</code>.
</p><p class="para">
As the above example also shows, you can use square brackets to create an array using an array literal. Empty array literals are written using an empty pair of square brackets and can be used to create an empty array of a specified type.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">emptyArray</span>: <span class="n"></span>[] = []</code></li>
</ul>
</div>
</section><p class="para">
You can create multidimensional arrays by chaining multiple sets of square brackets to the name of the base type of the elements. For example, you can create a three-dimensional array of integers using three sets of square brackets:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">array3D</span>: <span class="n"></span>[][][] = [[[<span class="m">1</span>, <span class="m">2</span>], [<span class="m">3</span>, <span class="m">4</span>]], [[<span class="m">5</span>, <span class="m">6</span>], [<span class="m">7</span>, <span class="m">8</span>]]]</code></li>
</ul>
</div>
</section><p class="para">
When accessing the elements in a multidimensional array, the left-most subscript index refers to the element at that index in the outermost array. The next subscript index to the right refers to the element at that index in the array that’s nested one level in. And so on. This means that in the example above, <code class="code-voice">array3D[0]</code> refers to <code class="code-voice">[[1, 2], [3, 4]]</code>, <code class="code-voice">array3D[0][1]</code> refers to <code class="code-voice">[3, 4]</code>, and <code class="code-voice">array3D[0][1][1]</code> refers to the value 4.
</p><p class="para">
For a detailed discussion of the Swift standard library <code class="code-voice">Array</code> type, see <span class="x-name"><a href="Collection Types.html#TP40014097-CH8-XID_135" data-id="TP40014097-CH8-XID_135">Arrays</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an array type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/array-type"></a>
<span class="syntax-def-name">
array-type
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span><code class="literal">[</code><code class="literal">]</code>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-type" data-id="//apple_ref/swift/grammar/array-type">array-type</a></span><code class="literal">[</code><code class="literal">]</code>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1011"></a>
<h3 class="section-name" tabindex="0">Optional Type</h3>
<p class="para">
The Swift language defines the postfix <code class="code-voice">?</code> as syntactic sugar for the named type <code class="code-voice">Optional<T></code>, which is defined in the Swift standard library. In other words, the following two declarations are equivalent:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">optionalInteger</span>: <span class="n"></span>?</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">optionalInteger</span>: <span class="n"></span>></code></li>
</ul>
</div>
</section><p class="para">
In both cases, the variable <code class="code-voice">optionalInteger</code> is declared to have the type of an optional integer. Note that no whitespace may appear between the type and the <code class="code-voice">?</code>.
</p><p class="para">
The type <code class="code-voice">Optional<T></code> is an enumeration with two cases, <code class="code-voice">None</code> and <code class="code-voice">Some(T)</code>, which are used to represent values that may or may not be present. Any type can be explicitly declared to be (or implicitly converted to) an optional type. When declaring an optional type, be sure to use parentheses to properly scope the <code class="code-voice">?</code> operator. As an example, to declare an optional array of integers, write the type annotation as <code class="code-voice">(Int[])?</code>; writing <code class="code-voice">Int[]?</code> produces an error.
</p><p class="para">
If you don’t provide an initial value when you declare an optional variable or property, its value automatically defaults to <code class="code-voice">nil</code>.
</p><p class="para">
Optionals conform to the <code class="code-voice">LogicValue</code> protocol and therefore may occur in a Boolean context. In that context, if an instance of an optional type <code class="code-voice">T?</code> contains any value of type <code class="code-voice">T</code> (that is, it’s value is <code class="code-voice">Optional.Some(T)</code>), the optional type evaluates to <code class="code-voice">true</code>. Otherwise, it evaluates to <code class="code-voice">false</code>.
</p><p class="para">
If an instance of an optional type contains a value, you can access that value using the postfix operator <code class="code-voice">!</code>, as shown below:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">optionalInteger</span> = <span class="m">42</span></code></li>
<li><code class="code-voice"><span class="vc">optionalInteger</span>! <span class="c">// 42</span></code></li>
</ul>
</div>
</section><p class="para">
Using the <code class="code-voice">!</code> operator to unwrap an optional that has a value of <code class="code-voice">nil</code> results in a runtime error.
</p><p class="para">
You can also use optional chaining and optional binding to conditionally perform an operation on an optional expression. If the value is <code class="code-voice">nil</code>, no operation is performed and therefore no runtime error is produced.
</p><p class="para">
For more information and to see examples that show how to use optional types, see <span class="x-name"><a href="The Basics.html#TP40014097-CH5-XID_428" data-id="TP40014097-CH5-XID_428">Optionals</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an optional type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/optional-type"></a>
<span class="syntax-def-name">
optional-type
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span><code class="literal">?</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1014"></a>
<h3 class="section-name" tabindex="0">Implicitly Unwrapped Optional Type</h3>
<p class="para">
The Swift language defines the postfix <code class="code-voice">!</code> as syntactic sugar for the named type <code class="code-voice">ImplicitlyUnwrappedOptional<T></code>, which is defined in the Swift standard library. In other words, the following two declarations are equivalent:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">implicitlyUnwrappedString</span>: <span class="n"></span>!</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">implicitlyUnwrappedString</span>: <span class="n"></span>></code></li>
</ul>
</div>
</section><p class="para">
In both cases, the variable <code class="code-voice">implicitlyUnwrappedString</code> is declared to have the type of an implicitly unwrapped optional string. Note that no whitespace may appear between the type and the <code class="code-voice">!</code>.
</p><p class="para">
You can use implicitly unwrapped optionals in all the same places in your code that you can use optionals. For instance, you can assign values of implicitly unwrapped optionals to variables, constants, and properties of optionals, and vice versa.
</p><p class="para">
As with optionals, if you don’t provide an initial value when you declare an implicitly unwrapped optional variable or property, it’s value automatically defaults to <code class="code-voice">nil</code>.
</p><p class="para">
Because the value of an implicitly unwrapped optional is automatically unwrapped when you use it, there’s no need to use the <code class="code-voice">!</code> operator to unwrap it. That said, if you try to use an implicitly unwrapped optional that has a value of <code class="code-voice">nil</code>, you’ll get a runtime error.
</p><p class="para">
Use optional chaining to conditionally perform an operation on an implicitly unwrapped optional expression. If the value is <code class="code-voice">nil</code>, no operation is performed and therefore no runtime error is produced.
</p><p class="para">
For more information about implicitly unwrapped optional types, see <span class="x-name"><a href="The Basics.html#TP40014097-CH5-XID_436" data-id="TP40014097-CH5-XID_436">Implicitly Unwrapped Optionals</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an implicitly unwrapped optional type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/implicitly-unwrapped-optional-type"></a>
<span class="syntax-def-name">
implicitly-unwrapped-optional-type
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span><code class="literal">!</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1017"></a>
<h3 class="section-name" tabindex="0">Protocol Composition Type</h3>
<p class="para">
A protocol composition type describes a type that conforms to each protocol in a list of specified protocols. Protocol composition types may be used in type annotations and in generic parameters.
</p><p class="para">
Protocol composition types have the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">protocol</span><<em class="variable-text">Protocol 1</em>, <em class="variable-text">Protocol 2</em>></pre></li>
</ul>
</div><p class="para">
A protocol composition type allows you to specify a value whose type conforms to the requirements of multiple protocols without having to explicitly define a new, named protocol that inherits from each protocol you want the type to conform to. For example, specifying a protocol composition type <code class="code-voice">protocol<ProtocolA, ProtocolB, ProtocolC></code> is effectively the same as defining a new protocol <code class="code-voice">ProtocolD</code> that inherits from <code class="code-voice">ProtocolA</code>, <code class="code-voice">ProtocolB</code>, and <code class="code-voice">ProtocolC</code>, but without having to introduce a new name.
</p><p class="para">
Each item in a protocol composition list must be either the name of protocol or a type alias of a protocol composition type. If the list is empty, it specifies the empty protocol composition type, which every type conforms to.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a protocol composition type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/protocol-composition-type"></a>
<span class="syntax-def-name">
protocol-composition-type
</span>
<span class="arrow">
→
</span><code class="literal">protocol</code><code class="literal"><</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/protocol-identifier-list" data-id="//apple_ref/swift/grammar/protocol-identifier-list">protocol-identifier-list</a></span><sub class="subscript">opt</sub></span><code class="literal">></code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/protocol-identifier-list"></a>
<span class="syntax-def-name">
protocol-identifier-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/protocol-identifier" data-id="//apple_ref/swift/grammar/protocol-identifier">protocol-identifier</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/protocol-identifier" data-id="//apple_ref/swift/grammar/protocol-identifier">protocol-identifier</a></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/protocol-identifier-list" data-id="//apple_ref/swift/grammar/protocol-identifier-list">protocol-identifier-list</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/protocol-identifier"></a>
<span class="syntax-def-name">
protocol-identifier
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-identifier" data-id="//apple_ref/swift/grammar/type-identifier">type-identifier</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1022"></a>
<h3 class="section-name" tabindex="0">Metatype Type</h3>
<p class="para">
A metatype type refers to the type of any type, including class types, structure types, enumeration types, and protocol types.
</p><p class="para">
The metatype of a class, structure, or enumeration type is the name of that type followed by <code class="code-voice">.Type</code>. The metatype of a protocol type—not the concrete type that conforms to the protocol at runtime—is the name of that protocol followed by <code class="code-voice">.Protocol</code>. For example, the metatype of the class type <code class="code-voice">SomeClass</code> is <code class="code-voice">SomeClass.Type</code> and the metatype of the protocol <code class="code-voice">SomeProtocol</code> is <code class="code-voice">SomeProtocol.Protocol</code>.
</p><p class="para">
You can use the postfix <code class="code-voice">self</code> expression to access a type as a value. For example, <code class="code-voice">SomeClass.self</code> returns <code class="code-voice">SomeClass</code> itself, not an instance of <code class="code-voice">SomeClass</code>. And <code class="code-voice">SomeProtocol.self</code> returns <code class="code-voice">SomeProtocol</code> itself, not an instance of a type that conforms to <code class="code-voice">SomeProtocol</code> at runtime. You can use a <code class="code-voice">dynamicType</code> expression with an instance of a type to access that instance’s runtime type as a value, as the following example shows:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">SomeBaseClass</span> {</code></li>
<li><code class="code-voice"> <span class="kt">class</span> <span class="kt">func</span> <span class="vc">printClassName</span>() {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"SomeBaseClass"</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">SomeSubClass</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">override</span> <span class="kt">class</span> <span class="kt">func</span> <span class="vc">printClassName</span>() {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"SomeSubClass"</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">someInstance</span>: <span class="n"></span> = <span class="vc">SomeSubClass</span>()</code></li>
<li><code class="code-voice"><span class="c">// someInstance is of type SomeBaseClass at compile time, but</span></code></li>
<li><code class="code-voice"><span class="c">// someInstance is of type SomeSubClass at runtime</span></code></li>
<li><code class="code-voice"><span class="vc">someInstance</span>.<span class="kt">dynamicType</span>.<span class="vc">printClassName</span>()</code></li>
<li><code class="code-voice"><span class="c">// prints "SomeSubClass"</span></code></li>
</ul>
</div>
</section><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a metatype type
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/metatype-type"></a>
<span class="syntax-def-name">
metatype-type
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span><code class="literal">.</code><code class="literal">Type</code>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span><code class="literal">.</code><code class="literal">Protocol</code>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1025"></a>
<h3 class="section-name" tabindex="0">Type Inheritance Clause</h3>
<p class="para">
A type inheritance clause is used to specify which class a named type inherits from and which protocols a named type conforms to. A type inheritance clause begins with a colon (<code class="code-voice">:</code>), followed by a comma-separated list of type identifiers.
</p><p class="para">
Class types may inherit from a single superclass and conform to any number of protocols. When defining a class, the name of the superclass must appear first in the list of type identifiers, followed by any number of protocols the class must conform to. If the class does not inherit from another class, the list may begin with a protocol instead. For an extended discussion and several examples of class inheritance, see <span class="x-name"><a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251">Inheritance</a></span>.
</p><p class="para">
Other named types may only inherit from or conform to a list of protocols. Protocol types may inherit from any number of other protocols. When a protocol type inherits from other protocols, the set of requirements from those other protocols are aggregated together, and any type that inherits from the current protocol must conform to all of those requirements.
</p><p class="para">
A type inheritance clause in an enumeration definition may be either a list of protocols, or in the case of an enumeration that assigns raw values to its cases, a single, named type that specifies the type of those raw values. For an example of an enumeration definition that uses a type inheritance clause to specify the type of its raw values, see <span class="x-name"><a href="Enumerations.html#TP40014097-CH12-XID_190" data-id="TP40014097-CH12-XID_190">Raw Values</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type inheritance clause
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-inheritance-clause"></a>
<span class="syntax-def-name">
type-inheritance-clause
</span>
<span class="arrow">
→
</span><code class="literal">:</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-inheritance-list" data-id="//apple_ref/swift/grammar/type-inheritance-list">type-inheritance-list</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-inheritance-list"></a>
<span class="syntax-def-name">
type-inheritance-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-identifier" data-id="//apple_ref/swift/grammar/type-identifier">type-identifier</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-identifier" data-id="//apple_ref/swift/grammar/type-identifier">type-identifier</a></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/type-inheritance-list" data-id="//apple_ref/swift/grammar/type-inheritance-list">type-inheritance-list</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH31-XID_1029"></a>
<h3 class="section-name" tabindex="0">Type Inference</h3>
<p class="para">
Swift uses type inference extensively, allowing you to omit the type or part of the type of many variables and expressions in your code. For example, instead of writing <code class="code-voice">var x: Int = 0</code>, you can omit the type completely and simply write <code class="code-voice">var x = 0</code>—the compiler correctly infers that <code class="code-voice">x</code> names a value of type <code class="code-voice">Int</code>. Similarly, you can omit part of a type when the full type can be inferred from context. For instance, if you write <code class="code-voice">let dict: Dictionary = ["A": 1]</code>, the compiler infers that <code class="code-voice">dict</code> has the type <code class="code-voice">Dictionary<String, Int></code>.
</p><p class="para">
In both of the examples above, the type information is passed up from the leaves of the expression tree to its root. That is, the type of <code class="code-voice">x</code> in <code class="code-voice">var x: Int = 0</code> is inferred by first checking the type of <code class="code-voice">0</code> and then passing this type information up to the root (the variable <code class="code-voice">x</code>).
</p><p class="para">
In Swift, type information can also flow in the opposite direction—from the root down to the leaves. In the following example, for instance, the explicit type annotation (<code class="code-voice">: Float</code>) on the constant <code class="code-voice">eFloat</code> causes the numeric literal <code class="code-voice">2.71828</code> to have type <code class="code-voice">Float</code> instead of type <code class="code-voice">Double</code>.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">e</span> = <span class="m">2.71828</span> <span class="c">// The type of e is inferred to be Double.</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">eFloat</span>: <span class="n"></span> = <span class="m">2.71828</span> <span class="c">// The type of eFloat is Float.</span></code></li>
</ul>
</div>
</section><p class="para">
Type inference in Swift operates at the level of a single expression or statement. This means that all of the information needed to infer an omitted type or part of a type in an expression must be accessible from type-checking the expression or one of its subexpressions.
</p>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794">Lexical Structure</a></p>
<p class="next-link"><a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655">Expressions</a></p>
<p class="copyright">Copyright © 2014 Apple Inc. All rights reserved. <a class="link" href="http://www.apple.com/legal/terms/site.html" target="_blank" rel="external">Terms of Use</a> | <a class="link" href="http://www.apple.com/privacy/" target="_blank" rel="external">Privacy Policy</a> | Updated: 2014-06-02
</p>
</section>
</article>
</div>
<div id="big_button"></div>
<footer id="footer">
<div class="content-wrapper">
</div>
<script src="./resource/devpubs-1.1.15.js" type="text/javascript" charset="utf-8"></script> </footer> <div id="blackout"> <div id="preload"></div> </div> </body></html>