You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: issue08/README.md
+194Lines changed: 194 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -360,10 +360,204 @@ this is what the code is doing:
360
360
361
361
the rest of this code is not magic. so really the important thing that you would want to take away with you is that typecasting in swift is done by an actual function call to an internal and private function called `imp___stubs__swift_dynamicCast`.
362
362
363
+
Unconditional Downcasting
364
+
===
365
+
Let's have a look at unconditional downcasting in swift. as i mentioned before, you can learn about downcasting on [apple's website](http://goo.gl/C15J0l) so i won't teach it here.
366
+
imagine you have the following two classes:
367
+
368
+
```swift
369
+
classVehicle{
370
+
funcid() ->Int{
371
+
return0xabcdefa
372
+
}
373
+
}
374
+
375
+
classCar : Vehicle{
376
+
overridefuncid() ->Int {
377
+
return0xabcdefc
378
+
}
379
+
}
380
+
```
381
+
382
+
and then we do this:
383
+
384
+
```swift
385
+
funcexample2(){
386
+
let v: Vehicle =Car()
387
+
let c = [v][0] as Car
388
+
println(c)
389
+
}
390
+
```
391
+
so the variable `c` is unconditionally downcasted from the first item in the array that only has `v` as its member. this array is `[Vehicle]` so if we want to get a `Car` object out of it, we have to downcast it, which is what we are doing. So let's see how Swift will compile this code:
as you can see, swift called a private hidden method called `imp___stubs__swift_dynamicCastClassUnconditional` to __downcast__ the `[Vehicle]` array's first item into a `Car` instance
550
+
551
+
Conditional Downcasting
552
+
===
553
+
554
+
555
+
363
556
Conclusion
364
557
===
365
558
1. Typecasting of values in Swift is done through an internal function called `imp___stubs__swift_dynamicCast`. Swift tends to typecast dynamically at runtime, rather than compile-time. This obviously has performance implications so keep that in mind.
366
559
2. An internal function called `__TTSSi_VSs7_StdoutS_Ss16OutputStreamType___TFSs5printU_Ss16OutputStreamType__FTQ_RQ0__T_` does the work for `println()` of `Int` values to the console.
560
+
3. Unconditional downcasts in Swift are done with a call to an internal function called `imp___stubs__swift_dynamicCastClassUnconditional`, at runtime.
0 commit comments