swift type system
TRANSCRIPT
![Page 1: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/1.jpg)
![Page 3: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/3.jpg)
![Page 4: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/4.jpg)
![Page 5: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/5.jpg)
![Page 6: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/6.jpg)
![Page 7: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/7.jpg)
![Page 8: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/8.jpg)
“Type systems are generally formulated as collections of rules for checking the ‘consistency’ of programs.”
Benjamin C. Pierce
![Page 9: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/9.jpg)
![Page 10: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/10.jpg)
![Page 11: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/11.jpg)
![Page 12: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/12.jpg)
![Page 13: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/13.jpg)
![Page 14: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/14.jpg)
is, as?, as! (as )
$ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> 1 is Any $R0: Bool = true 2> 1 is AnyObject $R1: Bool = false 3> import Foundation 4> 1 is Any $R2: Bool = true 5> 1 is AnyObject $R3: Bool = true // ???
![Page 15: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/15.jpg)
is, as?, as! (as )
$ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> [1] is [Any] $R0: Bool = true 2> [1] is [AnyObject] repl.swift:2:5: error: 'Bool' is not convertible to ‘[AnyObject]’ // ??? [1] is [AnyObject] ~~~~^~~~~~~~~~~~~~ 2> import Foundation 3> [1] is [Any] $R3: Bool = true 4> [1] is [AnyObject] $R4: Bool = true
![Page 16: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/16.jpg)
![Page 17: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/17.jpg)
![Page 18: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/18.jpg)
![Page 19: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/19.jpg)
![Page 20: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/20.jpg)
![Page 21: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/21.jpg)
nil
nil
![Page 22: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/22.jpg)
![Page 23: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/23.jpg)
![Page 24: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/24.jpg)
$ ghci GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help … Prelude> let square x = x * x Prelude> :info square square :: Num a => a -> a Prelude> square 10 100
![Page 25: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/25.jpg)
expression statement
// Objective-C NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] init];
// Swift let mutableAttributedString = NSMutableAttributedString()
![Page 26: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/26.jpg)
• 3: Int (default), IntXX, UIntXX, Double, Float, …
• 3.14159: Double (default), Float, …
![Page 27: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/27.jpg)
$ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> let x1 = 3 x1: Int = 3 // inferred to be Int 2> let x2 = Double(3) x2: Double = 3 // inferred to be Double 3> let x3: Double = 3 x3: Double = 3 // explicit type specified
4> let y1 = 3.14159 y1: Double = 3.1415899999999999 // inferred to be Double 5> let y2 = Float(3.14159) y2: Float = 3.14159012 // inferred to be Float 6> let y3: Float = 3.14159 y3: Float = 3.14159012 // explicit type specified
![Page 28: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/28.jpg)
import CoreGraphics
// func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint let x = CGFloat(1.0 / 3) // 1 / 3 = 0(?), 1.0 / 3 = 0.333 let y = CGFloat(1.0 / 3) // 1.0 / 3: Double to CGFloat let p = CGPointMake(x, y)
// CGFloat Swift // “1 / 3” “CGFloat / CGFloat -> CGFloat” // “1” “3” “CGFloat” let p = CGPointMake(1 / 3, 1 / 3)
![Page 29: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/29.jpg)
![Page 30: Swift Type System](https://reader030.vdocuments.net/reader030/viewer/2022021506/587123c71a28abe4448b5a9f/html5/thumbnails/30.jpg)