Skip to content

Commit aea05b0

Browse files
committed
docs: Add missing language modes to code blocks in DynamicCasting.md
1 parent 09a9a61 commit aea05b0

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

docs/DynamicCasting.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Where possible, each section includes machine-verifiable _invariants_ that can b
3636

3737
Casting an instance of a type to its own type will always succeed and return the original value unchanged.
3838

39-
```
39+
```swift
4040
let a: Int = 7
4141
a is Int // true
4242
a as? Int // Succeeds
@@ -109,13 +109,13 @@ Note: The associated `_ObjectiveCType` is constrained to be a subtype of `AnyObj
109109
In particular, this mechanism is equally available to the Swift implementation of Foundation on non-Apple platforms and the Objective-C Foundation on Apple platforms.
110110

111111
Example #1: Foundation extends the `Array` type in the standard library with an `_ObjectiveCBridgeable` conformance to `NSArray`. This allows Swift arrays to be cast to and from Foundation `NSArray` instances.
112-
```
112+
```swift
113113
let a = [1, 2, 3] // Array<Int>
114114
let b = a as? AnyObject // casts to NSArray
115115
```
116116

117117
Example #2: Foundation also extends each Swift numeric type with an `_ObjectiveCBridgeable` conformance to `NSNumber`.
118-
```
118+
```swift
119119
let a = 1 // Int
120120
// After the next line, b is an Optional<AnyObject>
121121
// holding a reference to an NSNumber
@@ -151,7 +151,7 @@ Casting to and from optional types will transparently unwrap optionals as much a
151151
Note: For "Optional Injection" above, the requirement that `t` is a non-nil instance of `T` implies that either `T` is not an `Optional` or that `T` is `Optional` and `t` has the form `.some(u)`
152152

153153
Examples
154-
```
154+
```swift
155155
// T and U are any two distinct types (possibly optional)
156156
// NO is any non-optional type
157157
let t: T
@@ -187,7 +187,7 @@ Note that "optional depth" here refers to the number of optional wrappers needed
187187
2. Otherwise, the value should inject with the greatest optional depth possible.
188188

189189
Examples
190-
```
190+
```swift
191191
// Depth preservation
192192
// The `.none` here has type `T?` with optional depth 1
193193
let t1: T???? = .some(.some(.some(.none)))
@@ -220,14 +220,14 @@ The result will be a new array where each `Int` in the original array has been i
220220

221221
However, if any component item cannot be cast, then the outer cast will also fail.
222222
For example, consider the following:
223-
```
223+
```swift
224224
let a: Array<Any> = [Int(7), "string"]
225225
a as? Array<Int> // Fails because "string" cannot be cast to `Int`
226226
```
227227

228228
Specifically, the casting operator acts for `Array` as if it were implemented as follows.
229229
In particular, note that an empty array can be successfully cast to any destination array type.
230-
```
230+
```swift
231231
func arrayCast<T,U>(source: Array<T>) -> Optional<Array<U>> {
232232
var result = Array<U>()
233233
for t in source {
@@ -249,7 +249,7 @@ Similar logic applies to `Set` and `Dictionary` casts.
249249
Note that the resulting `Set` or `Dictionary` may have fewer items than the original if the component casting operation converts non-equal items in the source into equal items in the destination.
250250

251251
Specifically, the casting operator acts on `Set` and `Dictionary` as if by the following code:
252-
```
252+
```swift
253253
func setCast<T,U>(source: Set<T>) -> Optional<Set<U>> {
254254
var result = Set<U>()
255255
for t in source {
@@ -382,7 +382,7 @@ For example, its metatype is named `AnyHashable.Type` and it does not have an ex
382382
Any protocol definition (except those that include an `associatedtype` property or which makes use of the `Self` typealias) has an associated existential type named after the protocol.
383383

384384
Specifically, assume you have a protocol definition
385-
```
385+
```swift
386386
protocol P {}
387387
```
388388

@@ -427,7 +427,7 @@ We call this type the "metatype of `T`".
427427
Technically, static variables or methods of a type belong to the `T.self` instance and are defined by the metatype of `T`:
428428

429429
Example:
430-
```
430+
```swift
431431
struct S {
432432
let ivar = 2
433433
static let svar = 1
@@ -447,7 +447,7 @@ However, in the following cases the metatype has a different name:
447447
* As explained above, although `AnyHashable` behaves like an existential type in some respects, its metatype is called `AnyHashable.Type`.
448448

449449
Example:
450-
```
450+
```swift
451451
// Metatype of a struct type
452452
struct S: P {}
453453
S.self is S.Type // always true
@@ -501,7 +501,7 @@ As with other existentials, casting _from_ an existential metatype is equivalent
501501
Casting _to_ an existential metatype succeeds whenever the source is a conforming metatype instance (or can be unwrapped to yield such a metatype instance).
502502

503503
Example
504-
```
504+
```swift
505505
protocol P {
506506
var ivar: Int { get }
507507
static svar: Int { get }
@@ -532,15 +532,15 @@ This is equivalent to saying that `P.self` is an instance of `P.Type`.
532532
(Remember that `P.self` is always an instance of `P.Protocol`.)
533533

534534
This is a concern for Swift because of the following construct, which attempts to invoke a generic `f` in a situation where the concrete instance clearly conforms to `P` but is represented as a `P` existential:
535-
```
535+
```swift
536536
func f<T:P>(t: T) { .. use t .. }
537537
let a : P = something
538538
f(a)
539539
```
540540
This construct is valid only if `T` conforms to `P` when `T = P`; that is, if `P` self-conforms.
541541

542542
A similar situation arises with generic types:
543-
```
543+
```swift
544544
struct MyGenericType<T: P> {
545545
init(_ value: T) { ... }
546546
}
@@ -591,7 +591,7 @@ These are some of the ways in which Swift 5.3 differs from the behavior describe
591591

592592
* Casts for which the target type is "more Optional" than the static source type previously produced errors. This disallowed all of the following: injecting an `Int` into an `Int?`, extracting an `Int?` from an opaque `Any` container, and casting an `Array<Int>` to an `Array<Int?>`. This document allows all of these.
593593

594-
```
594+
```swift
595595
let a = 7
596596
// Swift 5.3: error: cannot downcast to a more optional type
597597
// Specification: returns true
@@ -609,7 +609,7 @@ c is Int?
609609

610610
* An instance of a CoreFoundation type could sometimes be cast to a protocol defined on the companion Obj-C type and sometimes not. To make the behavior consistent, we had to choose one; having such casts always succeed seems more consistent with the general dual nature of Obj-C/CF types.
611611

612-
```
612+
```swift
613613
import Foundation
614614
protocol P {}
615615
extension NSString: P {}
@@ -625,7 +625,7 @@ print(b is P)
625625

626626
* The Swift 5.3 compiler asserts attempting to cast a struct to AnyObject
627627

628-
```
628+
```swift
629629
struct S {}
630630
let s = S()
631631
// Swift 5.3: Compiler crash (in asserts build)
@@ -635,7 +635,7 @@ s as? AnyObject
635635

636636
* `NSNumber()` does not cast to itself via `as?` in unoptimized builds
637637

638-
```
638+
```swift
639639
import Foundation
640640
let a = NSNumber()
641641
// true in 5.3 for optimized builds; false for unoptimized builds
@@ -644,7 +644,7 @@ print((a as? NSNumber) != nil)
644644

645645
* `Optional<NSNumber>` does not project
646646

647-
```
647+
```swift
648648
import Foundation
649649
let a: Optional<NSNumber> = NSNumber()
650650
// Swift 5.3: false
@@ -657,7 +657,7 @@ print(a as? NSNumber)
657657

658658
* Casting `NSNumber()` to `Any` crashes at runtime
659659

660-
```
660+
```swift
661661
import Foundation
662662
let a = NSNumber()
663663
// Swift 5.3: Runtime crash (both optimized and unoptimized builds)
@@ -667,7 +667,7 @@ print(a is Any)
667667

668668
* [#44896](https://github.com/apple/swift/issues/44896): CF types cannot be cast to protocol existentials
669669

670-
```
670+
```swift
671671
import Foundation
672672
protocol P {}
673673
extension CFBitVector : P {
@@ -682,7 +682,7 @@ print(CFBitVector.makeImmutable(from: [10,20]) is P)
682682

683683
* [#47129](https://github.com/apple/swift/issues/47129): Cannot cast `Optional<T> as Any` to protocol type. Note that this is a particular problem for reflection with weak fields, since `Mirror` reflects those as `Any` containing an `Optional` value.
684684

685-
```
685+
```swift
686686
protocol P {}
687687
class C: P {}
688688
let c: C? = C()
@@ -694,7 +694,7 @@ print(a is P)
694694

695695
* [#51469](https://github.com/apple/swift/issues/51469): `Any` containing `Optional<Any>` cannot cast to `Error`
696696

697-
```
697+
```swift
698698
struct MyError: Error { }
699699
let a: Any? = MyError()
700700
let b: Any = a
@@ -705,7 +705,7 @@ print(b is Error)
705705

706706
* [#48681](https://github.com/apple/swift/issues/48681): Inconsistent results for nested optionals
707707

708-
```
708+
```swift
709709
// Note: This issue includes many cases similar to the following
710710
let x: Int? = nil
711711
print(x as Int??) // ==> "Optional(nil)"
@@ -716,23 +716,23 @@ print((x as? Int??)!)
716716

717717
* `Error.self` does not fully self-conform
718718

719-
```
719+
```swift
720720
// Swift 5.3: Prints "false"
721721
// Specification: prints "true"
722722
print(Error.self is Error.Type)
723723
```
724724

725725
* Objective-C protocol metatypes do not fully self-conform
726726

727-
```
727+
```swift
728728
import Foundation
729729
let a = NSObjectProtocol.self
730730
print(a is NSObjectProtocol.Type)
731731
```
732732

733733
* [#44608](https://github.com/apple/swift/issues/44608): Cannot cast `Any` contents to a protocol type
734734

735-
```
735+
```swift
736736
protocol P {}
737737
class Foo: P {}
738738
let optionalFoo: Foo? = Foo()

0 commit comments

Comments
 (0)