@@ -81,7 +81,7 @@ Classes use dynamic dispatch for methods and property accesses by default. Thus
81
81
in the following code snippet, ``a.aProperty ``, ``a.doSomething() `` and
82
82
``a.doSomethingElse() `` will all be invoked via dynamic dispatch:
83
83
84
- ::
84
+ .. code-block :: swift
85
85
86
86
class A {
87
87
var aProperty: [Int ]
@@ -120,7 +120,7 @@ compiler can emit direct function calls instead of indirect calls. For instance
120
120
in the following ``C.array1 `` and ``D.array1 `` will be accessed directly
121
121
[# ]_. In contrast, ``D.array2 `` will be called via a vtable:
122
122
123
- ::
123
+ .. code-block :: swift
124
124
125
125
final class C {
126
126
// No declarations in class 'C' can be overridden.
@@ -155,7 +155,7 @@ and field accesses accordingly. For instance in the following,
155
155
``e.doSomething() `` and ``f.myPrivateVar ``, will be able to be accessed directly
156
156
assuming ``E ``, ``F `` do not have any overriding declarations in the same file:
157
157
158
- ::
158
+ .. code-block :: swift
159
159
160
160
private class E {
161
161
func doSomething () { ... }
@@ -210,7 +210,7 @@ counting if they contain, recursively, a reference type. By using value types
210
210
without reference types, one can avoid additional retain, release traffic inside
211
211
Array.
212
212
213
- ::
213
+ .. code-block :: swift
214
214
215
215
// Don't use a class here.
216
216
struct PhonebookEntry {
@@ -231,7 +231,7 @@ Advice: Use ContiguousArray with reference types when NSArray bridging is unnece
231
231
If you need an array of reference types and the array does not need to be
232
232
bridged to NSArray, use ContiguousArray instead of Array:
233
233
234
- ::
234
+ .. code-block :: swift
235
235
236
236
class C { ... }
237
237
var a: ContiguousArray <C> = [C (... ), C (... ), ... , C (... )]
@@ -248,7 +248,7 @@ container is mutated. For instance in the following, no copying will occur when
248
248
``c `` is assigned to ``d ``, but when ``d `` undergoes structural mutation by
249
249
appending ``2 ``, ``d `` will be copied and then ``2 `` will be appended to ``d ``:
250
250
251
- ::
251
+ .. code-block :: swift
252
252
253
253
var c: [Int ] = [ ... ]
254
254
var d = c // No copy will occur here.
@@ -260,7 +260,7 @@ object-reassignment in functions. In Swift, all parameters are passed in at +1,
260
260
i.e. the parameters are retained before a callsite, and then are released at the
261
261
end of the callee. This means that if one writes a function like the following:
262
262
263
- ::
263
+ .. code-block :: swift
264
264
265
265
func append_one (_ a : [Int ]) -> [Int ] {
266
266
var a = a
@@ -275,7 +275,7 @@ end of the callee. This means that if one writes a function like the following:
275
275
has no uses after ``append_one `` due to the assignment. This can be avoided
276
276
through the usage of ``inout `` parameters:
277
277
278
- ::
278
+ .. code-block :: swift
279
279
280
280
func append_one_in_place (a : inout [Int ]) {
281
281
a.append (1 )
@@ -298,7 +298,7 @@ Advice: Use wrapping integer arithmetic when you can prove that overflow cannot
298
298
In performance-critical code you can use wrapping arithmetic to avoid overflow
299
299
checks if you know it is safe.
300
300
301
- ::
301
+ .. code-block :: swift
302
302
303
303
a: [Int ]
304
304
b: [Int ]
@@ -326,7 +326,7 @@ behavior between ``MySwiftFunc<Int>`` and ``MySwiftFunc<String>`` are accounted
326
326
for by passing a different table of function pointers and the size abstraction
327
327
provided by the box. An example of generics:
328
328
329
- ::
329
+ .. code-block :: swift
330
330
331
331
class MySwiftFunc <T > { ... }
332
332
@@ -341,7 +341,7 @@ generic function specialized to the specific type. This process, called
341
341
*specialization *, enables the removal of the overhead associated with
342
342
generics. Some more examples of generics:
343
343
344
- ::
344
+ .. code-block :: swift
345
345
346
346
class MyStack <T > {
347
347
func push (_ element : T) { ... }
@@ -394,7 +394,7 @@ represented as values, so this example is somewhat realistic.
394
394
.. See Protocol-Oriented-Programming:
395
395
.. https://developer.apple.com/videos/play/wwdc2015-408/
396
396
397
- ::
397
+ .. code-block :: swift
398
398
399
399
protocol P {}
400
400
struct Node : P {
@@ -429,7 +429,7 @@ wrapping it in an array. This simple change has a major impact on the
429
429
performance of our tree data structure, and the cost of passing the array as an
430
430
argument drops from being O(n), depending on the size of the tree to O(1).
431
431
432
- ::
432
+ .. code-block :: swift
433
433
434
434
struct Tree : P {
435
435
var node: [P? ]
@@ -461,7 +461,7 @@ construct such a data structure:
461
461
.. More details in this blog post by Mike Ash:
462
462
.. https://www.mikeash.com/pyblog/friday-qa-2015-04-17-lets-build-swiftarray.html
463
463
464
- ::
464
+ .. code-block :: swift
465
465
466
466
final class Ref <T > {
467
467
var val: T
@@ -498,7 +498,7 @@ the reference Swift will increment the reference count of the ``next`` object
498
498
and decrement the reference count of the previous object. These reference
499
499
count operations are expensive and unavoidable when using Swift classes.
500
500
501
- ::
501
+ .. code-block :: swift
502
502
503
503
final class Node {
504
504
var next: Node?
@@ -523,7 +523,7 @@ instance held by the ``Unmanaged`` struct instance for the duration of the use
523
523
of ``Unmanaged `` (see `Unmanaged.swift `_ for more details) that keeps the instance
524
524
alive.
525
525
526
- ::
526
+ .. code-block :: swift
527
527
528
528
// The call to ``withExtendedLifetime(Head)`` makes sure that the lifetime of
529
529
// Head is guaranteed to extend over the region of code that uses Unmanaged
@@ -567,7 +567,7 @@ to retain or release non-trivial structures, which can be expensive.
567
567
If it makes sense to limit the adoption of protocols to classes then mark
568
568
protocols as class-only protocols to get better runtime performance.
569
569
570
- ::
570
+ .. code-block :: swift
571
571
572
572
protocol Pingable : AnyObject { func ping () -> Int }
573
573
@@ -582,7 +582,7 @@ considerations. Remember that any time one creates a binding for a
582
582
closure, one is forcing the compiler to emit an escaping closure,
583
583
e.x.:
584
584
585
- ::
585
+ .. code - block :: swift
586
586
587
587
let f: () -> () = { ... } // Escaping closure
588
588
// Contrasted with:
0 commit comments