Professional Documents
Culture Documents
Introduction to Scala
The “new” Java?
BEKK/UiO
July 4, 2009
Presentation Outline
Outline
Introduction
Concise Syntax
Object Orientation
Functional Programming
Various Stuff
Conclusion
Introducing Scala
What is Scala?
I 100% Object-oriented
I Functional programming
I Typing: static, strong, inferred/implicits
I Java bytecode → JVM
I .NET (CLR) Also available
I Extensibility
I Java-like syntax → Scala syntax
I Current version 2.7.5 → 2.8
Introducing Scala
A first example
class IntMath(val x: Int, val y: Int) {
def mul = x * y
}
println(test.x) // output: 3
println(test.sum) // output: 7
println(test.mul()) // output: 12
Introducing Scala
Syntax Examples
Imports
I Like Java, but more features
I Can be anywhere
I Import from packages, classes or objects
Syntax Examples
Method syntax
I . and () can be omitted for binary/unary methods
I Methods can have any name
I Operators are simply methods → full operator overloading
Syntax Examples
Type inferrence/implicits
I If type is obvious – no need to write it
I Implicits can be defined
I Implicits heavily used in std lib (RichInt etc.)
// Type inferrence
val a = 42 // Type = Int
val b = "hello world!" // Type = String
def add(x: Int, y: Int) = x + y // Return-type = Int
val sum = add(3, 5) // Type of sum = Int
// Implicits
class MyInt(val i: Int) { def doubleIt = i * 2 }
implicit def fromInt(i: Int) = new MyInt(i)
5.doubleIt // = 10
OO Introduction
Object Orientation
Classes
I Much like Java
I Contains fields and methods – can override eachother
I override keyword mandatory
I Can take parameters directly – constructor
Singleton Objects
I No static members in Scala → Singleton Objects
I Keyword object instead of class
I Companion class/object – same name
I Factory methods
I Other unique/static behaviour
Traits
Traits
I Encapsulates method and field definitions, much like classes
I A class can mix in any number of traits → multiple inheritance
I Widen thin interfaces to rich ones
I Define stackable modifications
trait Hello {
def hello { println("Hello!") }
}
trait Goodbye {
def bye { println("Bye bye!") }
}
Traits
trait Ordered[A] {
abstract def compare(that: A): Int
def <(that: A): Boolean = this.compare(that) < 0
def <=(that: A): Boolean = this.compare(that) <= 0
...
}
Traits
Generics
I Classes and traits can be generified
I Generics/type parameterization impl with erasure (like Java)
I Type parameters are required (not like Java)
I Variance – nonvariant, covariant and contravariant
I Upper and lower bounds
Abstract types
I Types as abstract members
I Much like type parameterization, different usage
I Generic types – reusable containers, collections ++
I Abstract types – premade subclasses, hides implementation
Functional programming
Mutable/immutable
Functions
Scala functions
I Higher-order functions – args and results
I Objects that implement scala.FunctionN traits
I Special syntax support with => operator
Functions
Closures/anonymous functions
I Scala – Anonymous functions
I Like anonymous classes in Java (and Scala)
I Nothing special with closures – just passing function object
I The (underscore) can be used to anonymize arguments
Functions
// Currying example
def curriedSum(i: Int)(j: Int) = i + j
curriedSum(2)(3) // Result 5
val fivePlus = curriedSum(5)_ // New func with 1 arg
fivePlus(6) // Result 11
Eivind Barstad Waaler BEKK/UiO
Introduction to Scala
Outline Introduction Concise Syntax Object Orientation Functional Programming Various Stuff Conclusion
Pattern Matching
Pattern matching
Pattern Matching
Pattern Matching
Pattern Matching
For expressions
I Generators, definitions and filters
I Can yield value – Range class
I A rewrite of methods map, flatMap and filter
for {
p <- persons // Generator
n = p.name // Definition
if(n startsWith "E") // Filter
} yield n
Interesting Tidbits
Actors api
I Simplify concurrent programming
I Hides threads – message based
I Immutable objects and functional style recommended!
import scala.actors.Actor._
val helloActor = actor {
while (true) {
receive {
case msg => println("hello message: " + msg)
}
}
}
helloActor ! "Hello World!!"
Interesting Tidbits
Continuations
I Coming in Scala 2.8
I Typical tasks:
I Asynchronous I/O with Java NIO
I Executors and thread pools
I Cross-request control flow in web applications
I New library functions, not keywords – shift and reset
I Complicated – different mindset
reset {
shift { k: (Int => Int) =>
k(k(k(7)))
} + 1
} * 2 // result: 20
Interesting Tidbits
Java integration
I Java is called from Scala directly
I @scala.reflect.BeanProperty annotation
import java.util.Date
val d = new Date
Rounding It Up
Conclusion
More Info:
http://www.scala-lang.org/