WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
Swift Type System
1.
2.
3.
4.
5.
6.
7.
8. “Type systems are generally formulated as collections of rules
for checking the ‘consistency’ of programs.”
Benjamin C. Pierce
9.
10.
11.
12.
13.
14. 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 // ???
15. 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
24. $ 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
27. $ 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
28. 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)