SWIFT THE implicit PARTS
MAXIM ZAKS 
@ICEX33
WHAT DO I MEAN BY implicit PARTS?
AGENDA 
▸ Values 
▸ Functions 
▸ Structures
Declaration of a constant 
let name : String = "Maxim";
Declaration of a constant with type inference and without semicolon 
let name = "Maxim"
Declaration of constant immutable array 
let names : Array<String> = ["Maxim", "Anton"] 
let names : [String] = ["Maxim", "Anton"] 
let names = ["Maxim", "Anton"] 
names.append("Fox") // Compile error
Declaration of mutable array (as variable) 
var names = ["Maxim", "Anton"] 
names.append("Fox") // ["Maxim", "Anton", "Fox"]
Declaration of Optional value 
var name : Optional<String> = "Maxim" 
name!.isEmpty == false // ??? 
var name : String? = nil 
name?.isEmpty == false // ???
Declaration of unwraped value if applicable 
var name : String? = nil 
if let _name = name { 
print("(_name)") 
} else { 
print("No name!") 
}
Declaration of value which is only optional in the beginning 
var name : ImplicitlyUnwrappedOptional<String> = "Maxim" 
name.isEmpty 
var name : String! = nil 
name.isEmpty // Runtime Exception
Decomposition of touples 
var a = 0, b = 0 
let touple = (20, 30) 
a = touple.0 
b = touple.1 
// Or just like this: 
(a, b) = touple
Convert from literal 
let url : NSURL = "http://nshipster.com/" 
"http://nshipster.com/".host
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) 
} 
}
▸ NilLiteralConvertible 
▸ BooleanLiteralConvertible 
▸ IntegerLiteralConvertible 
▸ FloatLiteralConvertible 
▸ StringLiteralConvertible / UnicodeScalarLiteralConvertible / 
ExtendedGraphemeClusterLiteralConvertible 
▸ ArrayLiteralConvertible 
▸ DictionaryLiteralConvertible
FUNCTIONS
Shortes function 
func f() -> Void {} 
func f() -> () {} 
func f() {}
Function without parameter label 
func f(_ v : Int) -> Int { return v + 1 } 
func f(v : Int) -> Int { return v + 1 } 
let v1 = f(10) // v1 = 11
Functoin with parameter label equal to parameter name 
func f(value value : Int) -> Int { return value + 1 } 
func f(#value : Int) -> Int { return value + 1 } 
let v1 = f(value : 10) // v1 = 11
In/Out parameter 
func f(inout v : Int) { v + 1 } 
var v = 10 
f(&v) // v = 11
Copy parameter in to variable 
func f(var v : [String]) -> [String] { 
v.append("Anton") 
return v 
} 
let a = ["Maxim"] 
let a2 = f(a) // a = ["Maxim"] , a2 = ["Maxim", "Anton"]
Rest parameter 
func sum(values:Int...) -> Int{ 
return values.reduce(0, +) 
} 
let sum1 = sum(1,2,3,4) // sum1 = 10 
let sum2 = sum() // sum2 = 0
Default parameter value 
func f(_ v : Int = 10) -> Int { 
return v + 1 
} 
let v1 = f() // v1 = 11 
let v2 = f(3) // v2 = 4
Curried function 
func f(a : Int)(b : Int) -> Int { 
return a + b 
} 
let v1 = f(1)(2)
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)
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)
Remove Closure types 
func f(a : Int) -> (Int -> Int) { 
return { 
b in 
return a + b 
} 
} 
let v1 = f(1)(2)
Remove return keyword 
func f(a : Int) -> (Int -> Int) { 
return { 
b in a + b 
} 
} 
let v1 = f(1)(2)
Remove parameter name 
func f(a : Int) -> (Int -> Int) { 
return { 
a + $0 
} 
} 
let v1 = f(1)(2)
Call a function with value 
func f(sum : Int) { 
print("sum is (sum)") 
} 
f(1 + 2) // f(3)
Call function with closure enables lazy evaluation 
func f(sum : () -> Int) { 
print("sum is (sum())") 
} 
f({1 + 2}) // lazy evaluation
Call function with auto closure enables lazy evaluation implicitly 
func f(sum : @autoclosure () -> Int) { 
print("sum is (sum())") 
} 
f(1 + 2) // still lazy evaluation
If last operator is a function you can take it out of parenthesis 
func f(a : Int, b: Int, operation : (Int,Int) -> Int) { 
print("sum is ( operation(a, b) )") 
} 
f(1,2) { 
$0 + $1 
}
Or keep it 
func f(a : Int, b: Int, operation : (Int,Int) -> Int) { 
print("sum is ( operation(a, b) )") 
} 
f(1, 2, +)
Operators are functions 
1 + 2 
infix operator + { 
associativity left 
precedence 140 
} 
func +(lhs: Int, rhs: Int) -> Int
Execution is based on precedence 
1 + 2 * 4 
infix operator + { 
associativity left 
precedence 140 
} 
infix operator * { 
associativity left 
precedence 150 
}
STRUCTURES
Touple with named values can be used as structs 
typealias MyPoint = (x:Int, y:Int) 
let point : MyPoint = (x:25, y:30) 
point.x // 25 
point.y // 30
Struct with implicit intializer 
struct MyPoint { 
let x : Int 
let y : Int 
} 
let point = MyPoint(x:25, y:30) 
point.x // 25 
point.y // 30
Methods are curried functions 
struct 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)()
TO BE 
CONTINUED...
THANK YOU! @ICEX33

Swift the implicit parts

  • 1.
  • 2.
  • 3.
    WHAT DO IMEAN BY implicit PARTS?
  • 4.
    AGENDA ▸ Values ▸ Functions ▸ Structures
  • 5.
    Declaration of aconstant let name : String = "Maxim";
  • 6.
    Declaration of aconstant with type inference and without semicolon let name = "Maxim"
  • 7.
    Declaration of constantimmutable array let names : Array<String> = ["Maxim", "Anton"] let names : [String] = ["Maxim", "Anton"] let names = ["Maxim", "Anton"] names.append("Fox") // Compile error
  • 8.
    Declaration of mutablearray (as variable) var names = ["Maxim", "Anton"] names.append("Fox") // ["Maxim", "Anton", "Fox"]
  • 9.
    Declaration of Optionalvalue var name : Optional<String> = "Maxim" name!.isEmpty == false // ??? var name : String? = nil name?.isEmpty == false // ???
  • 10.
    Declaration of unwrapedvalue if applicable var name : String? = nil if let _name = name { print("(_name)") } else { print("No name!") }
  • 11.
    Declaration of valuewhich is only optional in the beginning var name : ImplicitlyUnwrappedOptional<String> = "Maxim" name.isEmpty var name : String! = nil name.isEmpty // Runtime Exception
  • 12.
    Decomposition of touples var a = 0, b = 0 let touple = (20, 30) a = touple.0 b = touple.1 // Or just like this: (a, b) = touple
  • 13.
    Convert from literal let url : NSURL = "http://nshipster.com/" "http://nshipster.com/".host
  • 14.
    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) } }
  • 15.
    ▸ NilLiteralConvertible ▸BooleanLiteralConvertible ▸ IntegerLiteralConvertible ▸ FloatLiteralConvertible ▸ StringLiteralConvertible / UnicodeScalarLiteralConvertible / ExtendedGraphemeClusterLiteralConvertible ▸ ArrayLiteralConvertible ▸ DictionaryLiteralConvertible
  • 16.
  • 17.
    Shortes function funcf() -> Void {} func f() -> () {} func f() {}
  • 18.
    Function without parameterlabel func f(_ v : Int) -> Int { return v + 1 } func f(v : Int) -> Int { return v + 1 } let v1 = f(10) // v1 = 11
  • 19.
    Functoin with parameterlabel equal to parameter name func f(value value : Int) -> Int { return value + 1 } func f(#value : Int) -> Int { return value + 1 } let v1 = f(value : 10) // v1 = 11
  • 20.
    In/Out parameter funcf(inout v : Int) { v + 1 } var v = 10 f(&v) // v = 11
  • 21.
    Copy parameter into variable func f(var v : [String]) -> [String] { v.append("Anton") return v } let a = ["Maxim"] let a2 = f(a) // a = ["Maxim"] , a2 = ["Maxim", "Anton"]
  • 22.
    Rest parameter funcsum(values:Int...) -> Int{ return values.reduce(0, +) } let sum1 = sum(1,2,3,4) // sum1 = 10 let sum2 = sum() // sum2 = 0
  • 23.
    Default parameter value func f(_ v : Int = 10) -> Int { return v + 1 } let v1 = f() // v1 = 11 let v2 = f(3) // v2 = 4
  • 24.
    Curried function funcf(a : Int)(b : Int) -> Int { return a + b } let v1 = f(1)(2)
  • 25.
    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)
  • 26.
    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)
  • 27.
    Remove Closure types func f(a : Int) -> (Int -> Int) { return { b in return a + b } } let v1 = f(1)(2)
  • 28.
    Remove return keyword func f(a : Int) -> (Int -> Int) { return { b in a + b } } let v1 = f(1)(2)
  • 29.
    Remove parameter name func f(a : Int) -> (Int -> Int) { return { a + $0 } } let v1 = f(1)(2)
  • 30.
    Call a functionwith value func f(sum : Int) { print("sum is (sum)") } f(1 + 2) // f(3)
  • 31.
    Call function withclosure enables lazy evaluation func f(sum : () -> Int) { print("sum is (sum())") } f({1 + 2}) // lazy evaluation
  • 32.
    Call function withauto closure enables lazy evaluation implicitly func f(sum : @autoclosure () -> Int) { print("sum is (sum())") } f(1 + 2) // still lazy evaluation
  • 33.
    If last operatoris a function you can take it out of parenthesis func f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is ( operation(a, b) )") } f(1,2) { $0 + $1 }
  • 34.
    Or keep it func f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is ( operation(a, b) )") } f(1, 2, +)
  • 35.
    Operators are functions 1 + 2 infix operator + { associativity left precedence 140 } func +(lhs: Int, rhs: Int) -> Int
  • 36.
    Execution is basedon precedence 1 + 2 * 4 infix operator + { associativity left precedence 140 } infix operator * { associativity left precedence 150 }
  • 37.
  • 38.
    Touple with namedvalues can be used as structs typealias MyPoint = (x:Int, y:Int) let point : MyPoint = (x:25, y:30) point.x // 25 point.y // 30
  • 39.
    Struct with implicitintializer struct MyPoint { let x : Int let y : Int } let point = MyPoint(x:25, y:30) point.x // 25 point.y // 30
  • 40.
    Methods are curriedfunctions struct 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)()
  • 41.
  • 42.