1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Written by: Dr. JJ Shepherd
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Python Control of Flow.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Object Oriented Programming
1 CSC241: Object Oriented Programming Lecture No 27.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exceptions Handling Exceptionally Sticky Problems.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Introduction to Exception Handling and Defensive Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Exceptions. Why exceptions? We often strive for writing portable reusable code; we are able to detect errors, however our code may be used for many different.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
PHP Error Handling Section :I Source: 1.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Exceptions in the Java programming language J. W. Rider.
Exception handling.
16 Exception Handling.
Generics, Exceptions and Undo Command
MIT AITI 2003 Lecture14 Exceptions
Week 4 - Monday CS222.
Exceptions C++ Interlude 3
Topics Introduction to File Input and Output
Exceptions with Functions
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling In Text: Chapter 14.
Throwing and catching exceptions
Exception Handling Oo28.
Part B – Structured Exception Handling
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015

2 SWIFT 2.0 WWDC 2015

3 Swift 2.0: What’s new “Guard” Statement 1 Error Handling 2 “Defer” Statement 3 Protocol Extension 4

4 “GUARD” STATEMENT

5 “Guard” statement “A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.” guard condition else { statements }

6 Hey! I have already “if.. else” construction. Why do I need one more?

7 “Guard” statement func fooBinding(x: Int?) { if let x = x where x > 0 { // Do stuff with x x.description } // Value requirements not met, do something }

8 “Guard” statement func fooGuard(x: Int?) { guard let x = x where x > 0 else { // Value requirements not met, do something return } // Do stuff with x x.description }

9 “Guard” statement

10 “Guard” statement

11 ERROR HANDLING

12 Should I really handle all exceptional cases in application? It’s just such pain is the ass…

13 Error handling in the past In Objective-C there main error handling tool was NSError Exceptions were used to filter “programmer errors” and nobody really cared about catching these

14 ErrorType in Swift In Swift there is a special type (protocol) of enumeration which can be used to represent errors: ErrorType enum VendingMachineError: ErrorType { case InvalidSelection case InsufficientFunds(required: Double) case OutOfStock }

15 Throwing errors To indicate that a given function or method can fail, it has to be marked with the throws keyword Whenever a method/function is called that can throw an ErrorType it has to be used with try in front of it. As an alternative you can use try! to stop error distribution func vend(itemNamed name: String) throws { guard var item = inventory[name] else { throw VendingMachineError.InvalidSelection } … }

16 Catching Errors To catch errors, you can use the do {...} catch {...} control flow Catch can be defined for specific ErrorTypes If you don’t catch every possible ErrorType, than every un-handled error type will be thrown forward (travels up on the call stack) do { try vend(itemNamed: "Candy Bar") } catch VendingMachineError.InvalidSelection { print("Invalid Selection.") }

17 Benefits Since Swift has a strict type system (not even nil allowed for non optionals) you don’t have to declare a return value as an optional just because the function can fail Error distribution is a lot easier (try, catch, throw) You don’t have to use NSError** any more or create a delegate to notify about errors

18 “DEFER” STATEMENT

19 “Defer” statement Defer blocks can be used to delay the execution of a code block until it’s scope is executed It is not strictly related to the new error handling flow. It can be used for many reasons. It’s a method for cleaning up, even if your code failed at some point // Some scope: { // Get some resource. defer { // Release resource. } // Do things with the resource. // Possibly return early if an error occurs. } // Deferred code is executed at the end of the scope.

20 PROTOCOL EXTENSIONS

21 Protocol extensions let ints = [1,2,3,4,5] //Swift 1 map(filter(ints, {$0 % 1 == 0}), {$0 + 1}) //Swift 2 ints.map({$0 + 1}).filter({$0 % 1 == 0})

22 Protocol extensions extension CollectionType where Generator.Element : Equatable { public func indexOf(element: Generator.Element) -> Index? { for i in self.indices { if self[i] == element { return i } return nil }

23 Protocol extensions

24 Protocol extensions protocol ImportantProtocol { func doImportantStuff() } //default implementation for function extension ImportantProtocol { func doImportantStuff() { print("doing important stuff") }

25 PROTOCOL-ORIENTED PROGRAMMING Ha? What does it mean? What about objects and classes?

26 GOING TO OPEN-SOURCE How it will influence language growth

27 THANKS FOR YOUR ATTENTION!