scala: a brief tutorial

Download Scala: A brief tutorial

Post on 10-May-2015

363 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

Introduction to Scala showing the main concept as part of the Scala talk from SourceTalkDays

TRANSCRIPT

  • 1.(c) 2013 All rights reserved Oliver Szymanski Source-Knights.com jsxp.org Scala

2. Copyright 2010 Source-Knights.com Oliver Independent Java Enterprise Consultant jsxp.org and source-knights.com One of the founders of the Association of Java User Groups (ijug.eu) Speaker on conferences JavaOne, DOAG, JAX, SourceTalk, ... Author for IT-magazines Writer (Fantasy, Thriller, Science Fiction) oliver-szymanski.de 3. Copyright 2010 Source-Knights.com Overview Basics Data types Tuples Exceptions Modiers Special Types Operators Code ow Classes Functions Matching 4. Copyright 2010 Source-Knights.com Basics Data types literals like 3.5 or 3.5d or 3.5D (double), 6 (integer), 3.5f or 3.5F (oat) Basic types: Byte, Short, Int, Long, Float, Double, Char, String, Boolean String Blocks: println("""Welcome to Simply Scala. Click 'About' for more information.""") Alias: type MyAlias = String => Int Type intererence: only local 5. Copyright 2010 Source-Knights.com Basics Tuples val pair = ("answer", 42) // type: (String, Int) pair._1 // "answer" pair._2 // 42 val (i,c)=(1,'a') // careful with the brackets on the left side Tuples are in reality case classes: case class Tuple2[A, B](_1: A, _2: B) Special syntax: tuple with x1, ..., xn can be written (x1, ..., xn) Example: def divmod(x: Int, y: Int) = new Tuple2[Int, Int](x / y, x % y) 6. Copyright 2010 Source-Knights.com Basics Exceptions no checked exceptions in Scala use throws annotation such that Java code can catch exception @throws(classOf[IOException]) 7. Copyright 2010 Source-Knights.com Basics Modiers Annotations, each on their own line (lower case name), some java ones might be upper case Override modier (override) Access modier (protected, private) (also with [packagenames, classnames or this] Final modier (nal) 8. Copyright 2010 Source-Knights.com Basics Special types Nothing (sub type of all other types) Null (sub type of all reference types) Unit (empty return type) 9. Copyright 2010 Source-Knights.com Basics Operators Colon/Doublepoint The associativity of an operator is determined by its last character: Right-associative if ending with : Left-associative otherwise. x :: y :: z = x :: (y :: z) whereas x + y + z = (x + y) + z x :: y = y.::(x) whereas x + y = x.+(y) 10. Copyright 2010 Source-Knights.com Code ow If condition: val try1 = if (1==2) 8 else 9 // equals while(total < 17) total += 3 do { total+=3} while (total < 17) for(i