Concepts of programming languages Swift Lukas, Anvar, Roald, Bart, Lars [Faculty of Science Information and Computing Sciences] 1
Birth of a new language Swift… isn’t it beautiful? ◮ Developed in secret [Faculty of Science Information and Computing Sciences] 2
Birth of a new language Swift… isn’t it beautiful? ◮ Developed in secret ◮ Successor of Objective-C [Faculty of Science Information and Computing Sciences] 3
Objective C ◮ Introduced by NeXT Figure 1: Steve Jobs [Faculty of Science Information and Computing Sciences] 4
Objective C ◮ Introduced by NeXT Figure 2: Steve Jobs [Faculty of Science Information and Computing Sciences] 5
Objective C ◮ Introduced by NeXT ◮ Widely used after fjrst iPhone [Faculty of Science Information and Computing Sciences] 6
Objective C ◮ Introduced by NeXT ◮ Widely used after fjrst iPhone ◮ Steep learning curve [Faculty of Science Information and Computing Sciences] 7
NSString *test = [myString stringByAppendingString:@" Wouter"]; myString = "Goodmorning"; test = myString + " Wouter"; Objective C Objective-C: NSString *myString = @"Goodmorning"; Python: [Faculty of Science Information and Computing Sciences] 8
Swift Apple came to the rescue and developed Swift! ◮ Inferred typing [Faculty of Science Information and Computing Sciences] 9
var Dbl : Double = 3.14 var Str : String = "hallo" var Str = "hallo" //string var Dbl = 3.14 //double Swift - Inferred typing Normal: Inferred: [Faculty of Science Information and Computing Sciences] 10
Swift - Inferred typing Though Swift allows inferred typing it is still static typed [Faculty of Science Information and Computing Sciences] 11
Swift Apple came to the rescue and developed Swift! ◮ Inferred typing ◮ Neat Closures [Faculty of Science Information and Computing Sciences] 12
return s1 < s2 func sorting(_ s1: String, _ s2: String) -> Bool { } var reversedNames = names.sorted(by: sorting) var reversedNames = names.sorted { return s1 < s2 } Swift - Closures Without closures: With closures: [Faculty of Science Information and Computing Sciences] 13
Swift Apple came to the rescue and developed Swift! ◮ Inferred typing ◮ Neat Closures ◮ Protocols [Faculty of Science Information and Computing Sciences] 14
Swift - Pass by value or reference Protocols are passed by value. In Swift everything except for Classes is passed by value [Faculty of Science Information and Computing Sciences] 15
Swift Apple came to the rescue and developed Swift! ◮ Inferred typing ◮ Neat Closures ◮ Protocols ◮ Generics [Faculty of Science Information and Computing Sciences] 16
return variable.count func <T: count>length(_ variable: T) -> Int { } Swift Get length of generic type [Faculty of Science Information and Computing Sciences] 17
Swift Comparison with OO languages [Faculty of Science Information and Computing Sciences] 18
// Parameterless With Return Value - C# string sayHello() { return "Hello!"; } // Parameterless With Return Value - Swift func sayHello() -> String { return "Hello!" } Parameterless function ◮ Functions are the fjrst-class citizens in both C# and Swift; [Faculty of Science Information and Computing Sciences] 19
// C# string sayHello(string name) { // do something } // Swift func sayHello(name: String) -> String { // do something } Function with Parameters [Faculty of Science Information and Computing Sciences] 20
// C# - using a Struct struct Person { public string firstName; public string lastName; } return new Person { firstName = "Bill", lastName = "Gates" }; } Multiple Return Values Person getPerson() { [Faculty of Science Information and Computing Sciences] 21
// Swift func getPerson() -> (firstName: String, lastName: String) { return ("Steve", "Jobs") } Multiple Return Values [Faculty of Science Information and Computing Sciences] 22
// External Parameter Names // C# // no equivalent exists // Swift func combineName(firstName first: String, lastName last: String) { // accessing the parameters can be done by referencing // "first" and "last" respectively return first + " " + last; } Function Parameter Name Alias [Faculty of Science Information and Computing Sciences] 23
// C# int add (int a, int b) { return a + b; } Func<int, int, int> doMath = add; var total = doMath(19, 23); Function Types [Faculty of Science Information and Computing Sciences] 24
// Swift func add(a: Int, b: Int) -> Int { return a + b } var doMath: (Int, Int) -> Int = add var total = doMath(19, 23) Function Types [Faculty of Science Information and Computing Sciences] 25
// C# int doSomething(int a, int b) { var add = new Func<int, int, int>( (first, second) => { return first + second; } ); return add(a,b); } Nested Functions [Faculty of Science Information and Computing Sciences] 26
// Swift func doSomething(a: Int, b: Int) -> Int { func add(first: Int, second: Int) -> Int { return first + second } return add(a, b) } Nested Functions [Faculty of Science Information and Computing Sciences] 27
Swift Optionals [Faculty of Science Information and Computing Sciences] 28
Optionals What are Optionals? * Contains a value of some type or nil * Similar to Maybe in Haskell [Faculty of Science Information and Computing Sciences] 29
let longForm: Optional<Int> = Int("42") let shortForm: Int? = Int("42") Optionals How do we use Optionals? [Faculty of Science Information and Computing Sciences] 30
Optionals So why are they awesome? [Faculty of Science Information and Computing Sciences] 31
if let myLifesPurpose: String = Optional.none { print("its something: \(myLifesPurpose)") } else { print("guess ill die") } Optional Binding [Faculty of Science Information and Computing Sciences] 32
class SteamId { var id = "xXx_360n0Sc0P3_xXx"} class MacUser { var steamacc:SteamId?} let wouterSwierstra = MacUser() let id = wouterSwierstra.steamacc?.id print(id) // prints nil let id2 = wouterSwierstra.steamacc?.id ?? "Y U HAVE NO STEAM?" print(id2) // prints Y U HAVE NO STEAM? Optional Chaining [Faculty of Science Information and Computing Sciences] 33
Laziness ◮ Not standard in Swift ◮ keyword lazy ◮ more cumbersome to do than in Haskell [Faculty of Science Information and Computing Sciences] 34
class Probe { init() { print("YOU MUST CONSTRUCT ADDITIONAL PYLONS.") } } class Protoss { lazy var builder = Probe() } var Tassadar = Protoss() //will not print "YOU MUST CONSTRUCT ADDITIONAL PYLONS." Laziness [Faculty of Science Information and Computing Sciences] 35
class List{ let cur: Int init(){ cur = 3} lazy var next: List = List() func sum(cnt: Int) -> Int{ if(cnt == 0){return 0} return self.cur + self.sum(cnt: (cnt-1))} } let Buzz = List() print(Buzz.sum(cnt:140)) Laziness: infjnite list [Faculty of Science Information and Computing Sciences] 36
Swift Garbage Collection [Faculty of Science Information and Computing Sciences] 37
Garbage Collection Overview ◮ Memory deallocation ◮ Only objects that are passed by reference. Mark and Sweep ◮ Pauses program ◮ Iterates over memory at least twice ◮ Runtime delay [Faculty of Science Information and Computing Sciences] 38
Garbage Collection Reference Counting ◮ Stores amount of references to each memory chunk ◮ Memory deallocated when counter reaches 0 [Faculty of Science Information and Computing Sciences] 39
let name: String class Person { ... } var john = Person(name : "John Appleseed") Reference Counting Example Figure 3: Example of a reference to memory [Faculty of Science Information and Computing Sciences] 40
Reference Counting Pros ◮ Garbage is immediately noticed ◮ Operates at runtime ◮ Guarantees memory deallocation Cons ◮ Reference cycles ◮ Counters in memory (space overhead) ◮ Constant counting (speed overhead) ◮ Multi-node communication [Faculty of Science Information and Computing Sciences] 41
unit4A.tenant = john class Apartement { john.apartement = unit4A var unit4A = Apartement(unit : "4A") var john = Person(name : "John Appleseed") } var tenant : Person? let unit: String } ... var apartement = Apartement? let name: String class Person { Reference Cycle [Faculty of Science Information and Computing Sciences] 42
Reference Counting Reference Cycle Figure 4: Example of a reference cycle [Faculty of Science Information and Computing Sciences] 43
Reference Counting Preventing Cycles ◮ Additional info ◮ Strong, weak, unowned reference ◮ Link without a hold [Faculty of Science Information and Computing Sciences] 44
Reference Counting Weak Reference ◮ Does not prevent deallocation ◮ Set to nil ◮ Models optional properties ◮ For shorter target lifetime [Faculty of Science Information and Computing Sciences] 45
Recommend
More recommend