swift the implicit parts
TRANSCRIPT
SWIFT THE implicit PARTS
MAXIM ZAKS@ICEX33
WHAT DO I MEAN BY implicit PARTS?
AGENDA▸ Values▸ Functions▸ Structures
Declaration of a constantlet name : String = "Maxim";
Declaration of a constant with type inference and without semicolonlet name = "Maxim"
Declaration of constant immutable arraylet 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 valuevar name : Optional<String> = "Maxim"name!.isEmpty == false // ???
var name : String? = nilname?.isEmpty == false // ???
Declaration of unwraped value if applicablevar name : String? = nil
if let _name = name { print("\(_name)")} else { print("No name!")}
Declaration of value which is only optional in the beginningvar name : ImplicitlyUnwrappedOptional<String> = "Maxim"name.isEmpty
var name : String! = nilname.isEmpty // Runtime Exception
Decomposition of touplesvar a = 0, b = 0
let touple = (20, 30)
a = touple.0b = touple.1
// Or just like this:
(a, b) = touple
Convert from literallet 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 functionfunc f() -> Void {}func f() -> () {}func f() {}
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
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
In/Out parameterfunc f(inout v : Int) { v + 1 }
var v = 10f(&v) // v = 11
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"]
Rest parameterfunc sum(values:Int...) -> Int{ return values.reduce(0, +)}
let sum1 = sum(1,2,3,4) // sum1 = 10let sum2 = sum() // sum2 = 0
Default parameter valuefunc f(_ v : Int = 10) -> Int { return v + 1}
let v1 = f() // v1 = 11let v2 = f(3) // v2 = 4
Curried functionfunc 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 typesfunc f(a : Int) -> (Int -> Int) { return { b in return a + b }}
let v1 = f(1)(2)
Remove return keywordfunc f(a : Int) -> (Int -> Int) { return { b in a + b }}
let v1 = f(1)(2)
Remove parameter namefunc f(a : Int) -> (Int -> Int) { return { a + $0 }}
let v1 = f(1)(2)
Call a function with valuefunc f(sum : Int) { print("sum is \(sum)")}
f(1 + 2) // f(3)
Call function with closure enables lazy evaluationfunc f(sum : () -> Int) { print("sum is \(sum())")}
f({1 + 2}) // lazy evaluation
Call function with auto closure enables lazy evaluation implicitlyfunc 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 parenthesisfunc f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is \( operation(a, b) )")}
f(1,2) { $0 + $1}
Or keep itfunc f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is \( operation(a, b) )")}
f(1, 2, +)
Operators are functions1 + 2
infix operator + { associativity left precedence 140}
func +(lhs: Int, rhs: Int) -> Int
Execution is based on precedence1 + 2 * 4
infix operator + { associativity left precedence 140}
infix operator * { associativity left precedence 150}
STRUCTURES
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
Struct with implicit intializerstruct MyPoint { let x : Int let y : Int}
let point = MyPoint(x:25, y:30)
point.x // 25point.y // 30
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)()
TO BE CONTINUED...
THANK YOU!@ICEX33