SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

Similar documents
Swift. Introducing swift. Thomas Woodfin

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Scala : an LLVM-targeted Scala compiler

Swift, functional programming, and does it matter? Alexis

Porting Objective-C to Swift. Richard Ekle

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

CSCI-GA Scripting Languages

SWIFT BASICS

Apple s new Swift language

iphone Application Programming Lecture 3: Swift Part 2

Finding Bugs Using Xcode Runtime Tools

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

An introduction introduction to functional functional programming programming using usin Haskell

Xcode 6 and ios 8 What s New for Software Developers

iphone Application Programming Lecture 3: Swift Part 2

CS558 Programming Languages

Try the following example using Try it option available at the top right corner of the following sample code box:

CMSC 330: Organization of Programming Languages

Richard Mallion. Swift for Admins #TEAMSWIFT

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The Trouble with Types

Swift. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Let s Go! Akim D le, Etienne Renault, Roland Levillain. June 8, TYLA Let s Go! June 8, / 58

Introduction to Swift. Dr. Sarah Abraham

Objective-C and COCOA Applications

Go vs. Swift: The Languages of The Modern Tech Giants

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

CS558 Programming Languages

ios Application Development Lecture 2: Seminar and Unit 1

Optimizing Swift Performance Session 409

The Pyth Language. Administrivia

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

ITP 342 Mobile App Dev. Animation

Thread Sanitizer and Static Analysis

ITP 342 Advanced Mobile App Dev. Memory

Xcode and Swift CS 4720 Mobile Application Development

Xcode & Swift: Introduction

Mobile Application Programming. Swift Classes

COMP-520 GoLite Tutorial

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

ios: Objective-C Primer

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Using Scala in CS241

CSC324 Principles of Programming Languages

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Topic 9: Type Checking

Topic 9: Type Checking

Introductory ios Development

Senthil Kumaran S

Programming in Scala Second Edition

Software System Design and Implementation

TypeScript. Types. CS144: Web Applications

Perl 6 Hands-On Tutorial

CocoaHeads Aachen Server-side Swift

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

COMP327 Mobile Computing Session: Lecture Set 1a - Swift Introduction and the Foundation Framework Part 2

CS558 Programming Languages

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Introduce C# as Object Oriented programming language. Explain, tokens,

Announcement. Final Project Proposal Presentations and Updates

Introduction to OCaml

ITP 342 Mobile App Dev. Animation

ITP 342 Advanced Mobile App Dev. Memory

CS 371L - Mobile Computing (ios) Dr. William C. Bulko. CS 371L Mobile Computing (ios) Introduction

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

ITP 342 Mobile App Dev. Fundamentals

Programming Languages and Techniques (CIS120)

To use Xcode s Continuous Integration service with this Xcode beta, you need OS X with OS X Server 4.0.

G Programming Languages - Fall 2012

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

Introduction to Programming Using Java (98-388)

iphone Application Programming Lab 3: Swift Types and Custom Operator + A02 discussion

An introduction to Swift. Sasha

An introduction to C++ template programming

22c:111 Programming Language Concepts. Fall Types I

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Tokens, Expressions and Control Structures

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

...something useful to do with the JVM.

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Jython. An introduction by Thinh Le

Pierce Ch. 3, 8, 11, 15. Type Systems

Functional Programming. Introduction To Cool

KOTLIN/NATIVE + CLANG, TRAVEL NOTES NIKOLAY IGOTTI, JETBRAINS

Reverse Engineering Swift Apps. Michael Gianarakis Rootcon X 2016

BLM2031 Structured Programming. Zeyneb KURT

CS 2340 Objects and Design - Scala

A Fast Review of C Essentials Part I

Introduction to C++ with content from

Understanding Undefined Behavior

Short Notes of CS201

Introduction to Functional Programming and Haskell. Aden Seaman

Computer Components. Software{ User Programs. Operating System. Hardware

Transcription:

SWIFT! class Session { let title: String // constant non-optional field: can never be null and can never be changed var instruktør: Person? // variable optional field: null is permitted var attendees: [Person] = [] // array that can only contain Person instances var requirements: [String: Float] = [:] // dictionary of String to Float init(title: String) { self.title = title } // required initializer w/ named parameter } func ready() -> Bool { return attendees.count > 0 } // functon with return value func begin() { println("let's teach some Swift to \(attendees.count) eager learners!") } struct Person { let firstname, lastname, email: String } // immutable structure with automatic init let sesh = Session(title: "Swift!") // class initializer // struct initializer sesh.instruktør = Person(firstName: "Marc", lastname: "Prud'hommeaux", email: "marc@impathic.com") sesh.requirements["mac OS"] = 10.10 // dictionary value assignment sesh.requirements["xcode"] = 6.10 if training.ready() { // braces required, parenthesis optional training.begin() // semicolons optional } Marc Prud'hommeaux

About Me Marc Prud'hommeaux, American Indie Software Developer Neither French nor an Apple employee

Swift Announced at WWDC June 2014 It is: a new language for the future of Apple software development

Let's Build a Calculator App! https://gist.github.com/mprudhom

Thank You! Marc Prud'hommeaux <marc@impathic.com> Twitter: @mprudhom Apps: http://www.impathic.com

Agenda Swift vs. X Language Comparison Swift Language Features Swift Demo

Language Comparison

Very different! Objective-C

ObjC vs. Swift @interface Concatinator : NSObject @property NSString *separator; @end @implementation Concatinator - (NSString *)concat:(nsstring *)a withstring:(nsstring *)b { return [[a stringbyappendingstring:self.separator] stringbyappendingstring:b]; } @end

ObjC vs. Swift class Concatinator { var separator: String = "," } func concat(a: String, b: String) -> String { return a + separator + b }

Similar Java/C#

Java/C# vs. Swift class Concatinator { String separator = ","; } String concat(string a, String b) { return a + separator + b; }

Java/C# vs. Swift class Concatinator { var separator: String = "," } func concat(a: String, b: String) -> String { return a + separator + b }

Very similar! Scala

Scala vs. Swift class Concatinator { var separator: String = "," } def concat(a: String, b: String) : String = { return a + separator + b }

Scala vs. Swift class Concatinator { var separator: String = "," } func concat(a: String, b: String) -> String { return a + separator + b }

Type Systems Landscape static C OCaml Java Haskell Swift C# Scala Typescript Objective-C dynamic Assmebly JS Ruby Python Clojure weak strong Slide Credit: Martin Odersky, "The Trouble with Types"

Language Features

(Im)mutability let: constant var: variable

let vs. var let str: String = "Hello"

let vs. var let str: String = "Hello" str = str + " World" // illegal!

let vs. var var str: String = "Hello" str = str + " World" // legal

let vs. var var str: String = "Hello" str = nil // illegal!

Optionals Optionals permit nil-ability Designated with Type? or Optional<Type>

Optionals var str: String = "Hello" str = nil // illegal!

Optionals var str: String? = "Hello" str = nil // legal

Optionals var str: Optional<String> = "Hello" str = nil // legal

Generics Allow the parameterization of types

Generics var strings: Array<String> = ["a", "b", "c"]

Generics var strmap: [String : Int] = ["a": 1, "b": 2, "c": 3]

Type Inference Compiler figures out the type for you

Basic Type Inference var str: String = "abc"

Basic Type Inference var str = "abc"

Basic Type Inference var strs: Array<String> = ["a", "b", "c"]

Basic Type Inference var strs = ["a", "b", "c"]

Basic Type Inference var strmap: [String : Int] = ["a": 1, "b": 2, "c": 3]

Basic Type Inference var strmap = ["a": 1, "b": 2, "c": 3]

Simple Type Inference [1, 2, 3].reverse().map({ Double($0) }).filter({ $0 >= 2.0 }).map({ Float($0) })

Simple Type Inference let floats : Array<Float> = [1, 2, 3].reverse().map({ Double($0) }).filter({ $0 >= 2.0 }).map({ Float($0) })

Simple Type Inference let floats = [1, 2, 3].reverse().map({ Double($0) }).filter({ $0 >= 2.0 }).map({ Float($0) })

Complex Type Inference lazy([1, 2, 3]).reverse().map({ Double($0) }).filter({ $0 >= 0 }).map({ Float($0) })

Complex Type Inference let mapped : LazySequence <MapSequenceView <FilterSequenceView <MapCollectionView <RandomAccessReverseView <[Int]>, Double>>, Float>> = lazy([1, 2, 3]).reverse().map({ Double($0) }).filter({ $0 >= 0 }).map({ Float($0) })

Complex Type Inference let mapped = lazy([1, 2, 3]).reverse().map({ Double($0) }).filter({ $0 >= 0 }).map({ Float($0) })

Enumerations Fixed list of values With associated types!

Enums w/ Associated Types enum Stuff<T> { case Nothing case Something(T) } let stuff: Stuff = Stuff.Something("Hello") switch stuff { case.nothing: println("nothing at all") case.something(let value): println("something: \(value)") }

Structs structs are value types: they are copied when passed around or re-assigned vs. classes, which are reference types Swift's built-in String, Array, and Dictionary are structs! Unlike Cocoa's built-in NSString, NSArray, NSDictionary, which are reference types

class vs. struct class Concatinator { var separator: String = "," } func concat(a: String, b: String) -> String { return a + separator + b }

class vs. struct struct Concatinator { var separator: String = "," } func concat(a: String, b: String) -> String { return a + separator + b }

Functions & Closures func keyword First-class types! Functions within functions Functions returning functions Functions accepting functions arguments

Functions & Closures func add(a: Int, b: Int) -> Int { return a + b } let sum = add(1, 2)

Functions & Closures let add : (Int, Int) -> Int = { (a, b) in return a + b } let sum = add(1, 2)

Functions & Closures let add : (Int, Int) -> Int = { (a, b) in return a + b } let sum = add(1, 2)

Other Features Tuples (anonymous structs) Operator Overloading (operators as functions) Named parameters & default parameter values Pattern Matching ("switch on steroids") Function Currying (Haskell might be proud)

Shold I Adopt Swift? Pros Familiar Syntax for Java/C# devs Faster Data Structures Staticly Typed Playgrounds & REPL Good ObjC & C interop Functional Style Mature Runtime It's the Future Cons Unfamilliar to ObjC devs Inflexible Runtime Not Dynamically Typed Immature Tooling No C++ or Assembly interop Functional Style Immature Tooling (probably)

Environment

Runtime Same as Objective-C Runtime Shared memory with Cocoa classes Automatic Reference Counting (ARC)

Cocoa Interoperability Swift can access all Cocoa Frameworks Objective-C can access some Swift code

Development Environment Xcode Swift REPL Swift Playgrounds

Playground Demo

import Darwin struct Calc<T> { var lt: () -> T var op: (T, T) -> T var rt: () -> T } init(lt: () -> T, op: (T, T) -> T, rt: () -> T) { self.lt = lt self.op = op self.rt = rt } init(const: T) { self.init(lt: { const }, op: { lt, rt in lt }, rt: { const }) } var run: (() -> T) { return { self.op(self.lt(), self.rt()) } } mutating func push(op: (T, T)->T, value: T) { self = Calc(lt: self.run, op: op, rt: { value }) } infix operator ** { associativity left precedence 170 } func ** (num: Double, power: Double) -> Double { return pow(num, power) } var clc = Calc(const: 1.0) clc.push(+, value: 2) clc.push(**, value: 3) clc.run()

Swift Adoption Considerations

Immature Tooling Xcode SourceKit!

Uncertain Future Apple's history: Java, Python, Ruby, GC

Compiler Limitations enums with associated types: "unimplemented IR generation feature non-fixed multi-payload enum layout" class variables: "class variables not yet supported" no currying struct methods: "partial application of struct method is not allowed" perplexing error messages: "'() -> T' is not a subtype of 'UInt8'"

Future Compatibility Compiled Swift code will continue to run Language syntax will change

No Garbage Collection ARC requires that you manually break reference cycles...or declare one side to be weak or unowned

There is none! Error Handling

Concurrency None! No locks, no synchronized, no nothing

Closedness LLVM & clang are open-source Swift standard library is not

Non-Portability Unlikely to be running on non-apple devices anytime soon

Static vs. Dynamic Swift eschews Objective-C's dynamic dispatch No message passing No objc_msgsend

No C++/Assembly Interop Swift interoperates with Objective-C & C Does not interoperate with C++ or assembly

Objective-C to Swift interoperability Swift fully interoperates with Objective-C Objective-C partially interoperates with Swift

OS Support ios 7+ MacOS 10.10+

Thank You! Marc Prud'hommeaux <marc@impathic.com> Twitter: @mprudhom Apps: http://www.impathic.com