Swift the implicit parts

42
SWIFT THE implicit PARTS

Transcript of Swift the implicit parts

Page 1: Swift the implicit parts

SWIFT THE implicit PARTS

Page 2: Swift the implicit parts

MAXIM ZAKS@ICEX33

Page 3: Swift the implicit parts

WHAT DO I MEAN BY implicit PARTS?

Page 4: Swift the implicit parts

AGENDA▸ Values▸ Functions▸ Structures

Page 5: Swift the implicit parts

Declaration of a constantlet name : String = "Maxim";

Page 6: Swift the implicit parts

Declaration of a constant with type inference and without semicolonlet name = "Maxim"

Page 7: Swift the implicit parts

Declaration of constant immutable arraylet names : Array<String> = ["Maxim", "Anton"]let names : [String] = ["Maxim", "Anton"]let names = ["Maxim", "Anton"]

names.append("Fox") // Compile error

Page 8: Swift the implicit parts

Declaration of mutable array (as variable)var names = ["Maxim", "Anton"]

names.append("Fox") // ["Maxim", "Anton", "Fox"]

Page 9: Swift the implicit parts

Declaration of Optional valuevar name : Optional<String> = "Maxim"name!.isEmpty == false // ???

var name : String? = nilname?.isEmpty == false // ???

Page 10: Swift the implicit parts

Declaration of unwraped value if applicablevar name : String? = nil

if let _name = name { print("\(_name)")} else { print("No name!")}

Page 11: Swift the implicit parts

Declaration of value which is only optional in the beginningvar name : ImplicitlyUnwrappedOptional<String> = "Maxim"name.isEmpty

var name : String! = nilname.isEmpty // Runtime Exception

Page 12: Swift the implicit parts

Decomposition of touplesvar a = 0, b = 0

let touple = (20, 30)

a = touple.0b = touple.1

// Or just like this:

(a, b) = touple

Page 13: Swift the implicit parts

Convert from literallet url : NSURL = "http://nshipster.com/""http://nshipster.com/".host

Page 14: Swift the implicit parts

extension NSURL: StringLiteralConvertible { public class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self { return self(string: value) }

public class func convertFromStringLiteral(value: String) -> Self { return self(string: value) }}

Page 15: Swift the implicit parts

▸ NilLiteralConvertible▸ BooleanLiteralConvertible▸ IntegerLiteralConvertible▸ FloatLiteralConvertible

▸ StringLiteralConvertible / UnicodeScalarLiteralConvertible / ExtendedGraphemeClusterLiteralConvertible

▸ ArrayLiteralConvertible▸ DictionaryLiteralConvertible

Page 16: Swift the implicit parts

FUNCTIONS

Page 17: Swift the implicit parts

Shortes functionfunc f() -> Void {}func f() -> () {}func f() {}

Page 18: Swift the implicit parts

Function without parameter labelfunc f(_ v : Int) -> Int { return v + 1 }func f(v : Int) -> Int { return v + 1 }

let v1 = f(10) // v1 = 11

Page 19: Swift the implicit parts

Functoin with parameter label equal to parameter namefunc f(value value : Int) -> Int { return value + 1 }func f(#value : Int) -> Int { return value + 1 }

let v1 = f(value : 10) // v1 = 11

Page 20: Swift the implicit parts

In/Out parameterfunc f(inout v : Int) { v + 1 }

var v = 10f(&v) // v = 11

Page 21: Swift the implicit parts

Copy parameter in to variablefunc f(var v : [String]) -> [String] { v.append("Anton") return v}

let a = ["Maxim"]let a2 = f(a) // a = ["Maxim"] , a2 = ["Maxim", "Anton"]

Page 22: Swift the implicit parts

Rest parameterfunc sum(values:Int...) -> Int{ return values.reduce(0, +)}

let sum1 = sum(1,2,3,4) // sum1 = 10let sum2 = sum() // sum2 = 0

Page 23: Swift the implicit parts

Default parameter valuefunc f(_ v : Int = 10) -> Int { return v + 1}

let v1 = f() // v1 = 11let v2 = f(3) // v2 = 4

Page 24: Swift the implicit parts

Curried functionfunc f(a : Int)(b : Int) -> Int { return a + b}

let v1 = f(1)(2)

Page 25: Swift the implicit parts

Returning a closure (idea behind currying)func f(a : Int) -> (Int -> Int) { let f1 = { (b: Int) -> Int in return a + b } return f1}

let v1 = f(1)(2)

Page 26: Swift the implicit parts

Returning a closure (idea behind currying)func f(a : Int) -> (Int -> Int) { return { (b: Int) -> Int in return a + b }}

let v1 = f(1)(2)

Page 27: Swift the implicit parts

Remove Closure typesfunc f(a : Int) -> (Int -> Int) { return { b in return a + b }}

let v1 = f(1)(2)

Page 28: Swift the implicit parts

Remove return keywordfunc f(a : Int) -> (Int -> Int) { return { b in a + b }}

let v1 = f(1)(2)

Page 29: Swift the implicit parts

Remove parameter namefunc f(a : Int) -> (Int -> Int) { return { a + $0 }}

let v1 = f(1)(2)

Page 30: Swift the implicit parts

Call a function with valuefunc f(sum : Int) { print("sum is \(sum)")}

f(1 + 2) // f(3)

Page 31: Swift the implicit parts

Call function with closure enables lazy evaluationfunc f(sum : () -> Int) { print("sum is \(sum())")}

f({1 + 2}) // lazy evaluation

Page 32: Swift the implicit parts

Call function with auto closure enables lazy evaluation implicitlyfunc f(sum : @autoclosure () -> Int) { print("sum is \(sum())")}

f(1 + 2) // still lazy evaluation

Page 33: Swift the implicit parts

If last operator is a function you can take it out of parenthesisfunc f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is \( operation(a, b) )")}

f(1,2) { $0 + $1}

Page 34: Swift the implicit parts

Or keep itfunc f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is \( operation(a, b) )")}

f(1, 2, +)

Page 35: Swift the implicit parts

Operators are functions1 + 2

infix operator + { associativity left precedence 140}

func +(lhs: Int, rhs: Int) -> Int

Page 36: Swift the implicit parts

Execution is based on precedence1 + 2 * 4

infix operator + { associativity left precedence 140}

infix operator * { associativity left precedence 150}

Page 37: Swift the implicit parts

STRUCTURES

Page 38: Swift the implicit parts

Touple with named values can be used as structstypealias MyPoint = (x:Int, y:Int)

let point : MyPoint = (x:25, y:30)

point.x // 25point.y // 30

Page 39: Swift the implicit parts

Struct with implicit intializerstruct MyPoint { let x : Int let y : Int}

let point = MyPoint(x:25, y:30)

point.x // 25point.y // 30

Page 40: Swift the implicit parts

Methods are curried functionsstruct MyName { let name : String

init(_ n : String) { name = n }

func desc()->String{ return "My name is: \(name)" }}let myName = MyName("Maxim")myName.desc()MyName.desc(myName)()

Page 41: Swift the implicit parts

TO BE CONTINUED...

Page 42: Swift the implicit parts

THANK YOU!@ICEX33