1. The Swift Compiler and
the Standard Library
Talk at Bangalore Swift Meetup Oct 4 2014 @HasGeek
2. I am
Santosh Rajan
santoshrajan.com
twitter.com/santoshrajan
github.com/santoshrajan
in.linkedin.com/in/santoshrajan
3. Chris Lattner
• Author of LLVM since 2002
• Joined Apple 2005
• Author Of the Swift Language since 2010
4. Low Level Virtual Machine
(LLVM)
C C++ Objective C ?
Low Level Language
LLVM
Machine Code
X86 PowerPC ARM SPARC Other..
5. Problem
• C, C++, Objective C too hard for App developers
Solution
• Simple Language syntax like JavaScript
• Static Typing to allow compiler optimisations
• Higher order functions to enable closures
• Host of other features …
6. Low Level Virtual Machine
(LLVM)
C C++ Objective C Swift
Low Level Language
LLVM
Machine Code
X86 PowerPC ARM SPARC Other..
7. Where is Swift?
$ xcrun -f swift
/Applications/Xcode.app/Contents/Developer/Toolchains/
XcodeDefault.xctoolchain/usr/bin/swift
Set an Alias
alias swift=/Applications/Xcode.app/Contents/Developer/
Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
8. REPL
$ swift
Welcome to Swift! Type :help for assistance.
1> 2 + 2
$R0: Int = 4
2>
Run
$ echo "println("Hello World")" > hello.swift
$ swift hello.swift
Hello World
9. Where is the Swift Compiler?
$ xcrun -f swiftc
/Applications/Xcode.app/Contents/Developer/Toolchains/
XcodeDefault.xctoolchain/usr/bin/swiftc
Set an Alias
alias swiftc=/Applications/Xcode.app/Contents/
Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/
swiftc
10. Compile and Run
$ swiftc hello.swift -o hello
$ ./hello
Hello World
Generate LLVM bytecode
$ swiftc -emit-bc hello.swift -o hello.bc
11. Command Line Arguments
// edit hello.swift to
!
dump(Process.arguments)
$ swift hello.swift foo bar “hello world”
▿ 4 elements
— [0]: hello.swift
— [1]: foo
— [2]: bar
— [3]: hello world
13. Types
Create Types Using
• Enums
• Structs
• Classes
Enums and Struct are value types.
Classes are reference Types.
14. Enums
Use Enums when the Values of the Type are
limited. e.g. Planets.
Enum Bit {
case Zero
case One
func Successor() -> Bit
func predecessor() -> Bit
}
15. Structs
All Basic Value Types in Swift are defined using
Structs. e.g. Bool, Int, Float, Double, Array, Dictionary.
Struct Array<T> {
var count: Int { get }
var isEmpty: Bool { get }
mutating func append(newElement: T)
mutating func insert(newElement: T, atIndex i: Int)
}
Instance Variables
Instance Methods
16. Classes
Classes are reference Types. Use Classes when
instances are Objects.
18. Protocols
Protocols are templates for Structs and Classes.
Like interfaces in Java
Any Type that implements the Printable protocol
should implement the instance variable `description`
which is a getter that returns a String.
!
protocol Printable {
var description: String { get }
}
19. SequenceType Protocol
Types implementing this protocol can be used
in for loops. e.g. String, Array, Dictionary
protocol SequenceType {
func generate() -> GeneratorType
}
GeneratorType Protocol
protocol GeneratorType {
mutating func next() -> Element?
}
20. SequenceType Example
var arr = ["Hello", "how", "are", “you"]
var generator = arr.generate()
while let elem = generator.next() {
println(elem)
}
Is the same as
for elem in arr {
println(elem)
}
In fact the swift compiler will will compile the `for loop` into the
`while loop` above.
23. Global Functions
Comes with over 70 Global built in functions.
Will cover some here.
Printing to an output stream. Default
standard out.
dump(object)
print(object)
println(object)
24. assert
Takes a condition expression that evaluates to
true or false, and a message.
// file assert.swift
assert(Process.arguments.count > 1, “Argument required”)
println(“Argument Supplied”)
!
!
$ swift assert.swift // assertion failed: Argument required.
$ swift assert.swift foo // Argument Supplied
25. contains
Takes a sequence and calls the predicate
function with every element.
var arr = [“Foo”, “Bar”, “Baz”, “Bez”]
if contains(arr, {$0 == “Baz”}) {
println(“Array contains Baz”)
}
26. enumerate
Returns an EnumerateGenerater. The Generator
returns a tuple containing index and Element.
var intarray = [1, 2, 3, 4]
for (index, element) in enumerate(intarray) {
println(“Item (index): (element)”)
}
27. map
func square(x: Int) -> Int {
return x * x
}
var num: Int? = 10
println(map(num, square)) // prints Optional(100)
wut?
// Haskells fmap for optionals
func map<T, U>(x: T?, f: (T) -> U) -> U?