Types are important? Yes they are! And they can form the whole behaviour of a language - like Swift. This talk deals with the very type essentials and gives a first glance under the hood of the Swift type system.
programming languages and a basic understanding of type systems. Yes, we will see code – because programming! ~LVL300-400 It’s not impolite to leave now – no kitten memes to miss here!
Swift 1.1 2015-04-08 Swift 1.2 2015-09-16 Swift 2 2015-10-20 Swift 2.1 2015-12-03 open sourced 2016-03-21 Swift 2.2 2016-06-12 Swift 2.3 2016-09-13 Swift 3 * 2016-10-27 Swift 3.0.1 * Compiler for Swift 3 and 2.3 offered in Xcode 8 Code adjustments No downwards compatiblity
heavy inheritance Super loose coupling Nice separation of concerns Clean architecture … and there are just two types of types! Named types & compound types* *which can contain named types
12, y: 50) greatTuple = (0,0) print (greatTuple.0 + “==“ + greatTuple.x) greatTuple = (xCoord: 10, yCoord: 20) greatTuple = (x: “manu”, y: 20) Tuple Type Ad hoc type definition for named multiple values with type safety
foreground = randomForegroundColor() let background = randomBackgroundColor() return (foreground, background) } let colorScheme = randomColorScheme() view.backgroundColor = colorScheme.bg Tuple Type Use to return multiple values from a function
var point = Point(10, 12) typealias Point = (x: Int, y: Int) var myPoint = Point(x: 10, y: 12) var myOtherPoint = Point(10, 12) var myRealPoint = (10, 12) typealias CGPoint = (String, String) var thePoint = CGPoint(“this is”, “dangerous”) Tuple Type Give them names… … with typealias
Int) {} struct Person { firstname : String lastname : String } func nameForPerson (withID: Int) -> (name: String, age: Int) {} func nameForPerson (withID: Int) -> (Person) {} func nameForPerson (withID: Int) -> Person {} func detailsForPerson (withID: Int) -> (obj: Person, age: Int) {} Function Type Defines the type of a function or method. Combines the parameter and return type with -> Can be composed of named and compound types again
func goAhead (input: Int) -> Int { return input + 1 } func goBack (input: Int) -> Int { return input - 1 } return ahead ? goAhead : goBack } var stepsToHome = -3 let goHome = whereToGo(ahead: stepsToHome < 0) while stepsToHome != 0 { print("steps to home: \(abs(stepsToHome))") stepsToHome = goHome(stepsToHome) } Function Type Global functions vs. nested functions… … and their strange looking return types. (Bool) -> (Int) -> Int in inner in out
stepsToHome () -> Int { return steps } var f = goHome f = stepsToHome // type of f is (Bool) -> Void Variadic parameters as arrays func printAll(_ numbers: Int...) -> Int { for number in numbers { print ( “the number \(number)”) } }
ImplicitlyUnwrappedOptional<Wrapped> : ExpressibleByNilLiteral { /// The absence of a value. Typically written using the nil literal, `nil`. case none /// The presence of a value, stored as `Wrapped`. case some(Wrapped) /// Creates an instance that stores the given value. public init(_ some: Wrapped) /// Creates an instance initialized with `nil`. /// /// Do not call this initializer directly. It is used by the compiler when /// you initialize an `Optional` instance with a `nil` literal. For example: /// let i: Index! = nil public init(nilLiteral: ()) }
Swift version tied to Xcode version Swift based frameworks / libraries Longer compilation / building time Getting used to the ”swifty way” Swift, YEY of NEY … ? Have fun with Short and descriptive syntax (if you use it) Type safety Strong compiler time validation … definitively YEY!