This document provides an introduction and overview of the Scala programming language. It discusses how Scala is a scalable language that is pure object-oriented, statically typed, functional, and runs on the JVM. It highlights some of Scala's key features like everything being an object, no primitive types, and operations being method calls. Motivations for using Scala over Java are presented, including support for functions and closures, an extended type system, and properties of essence over ceremony and extended control structures.
1. Scala
... a Scalable Language
06.10.2009
XPUG Rhein Main
Mario Gleichmann
Mario Gleichmann XPUG Rhein/Main
2. Introduction
Mario Gleichmann
site: www.mg-informatik.de
blog: 'brain driven development'
gleichmann.wordpress.com
mail: mario.gleichmann@mg-informatik.de
Mario Gleichmann XPUG Rhein/Main
3. Motivation
Open your mind ...
• Scala vs. Java 9
• Functional programming for the imperative mind
• Discover the (new) possibilities ...
Mario Gleichmann XPUG Rhein/Main
4. Motivation
Open your mind ...
“If Java programmers want to
• Scala vs. Java 9
use features that aren't present
• Functional programming forthe language, I think they're
in the imperative mind
probably best off using another
• Discover the (new) possibilities ...
language that targets the JVM,
such as Scala and Groovy“
Joshua Bloch
Mario Gleichmann XPUG Rhein/Main
5. Motivation
Open your mind ...
● Functions & Closures
• Scala vs. Java 9 ●
Extended Type System
• Functional programming for the imperative mind
● Extended Module System
• ●
Properties
Discover the (new) possibilities ...
●
Essence over Ceremony
● Extended Control Structs
Mario Gleichmann XPUG Rhein/Main
6. Motivation
Open your mind ...
• Scala vs. Java 9 “If i were to pick a language
• Functional programming foruse today other than Java,
to the imperative mind
it would be Scala“
• Discover the (new) possibilities ...
James Gosling
Mario Gleichmann XPUG Rhein/Main
7. Motivation
Open your mind ...
• Functional programming for the imperative mind
• Discover the possibilities ...
Imparative programming
is a programming paradigm
that describes computation
in terms of statements
that change a programs state
Mario Gleichmann XPUG Rhein/Main
8. Motivation
Open your mind ...
• Functional programming for the imperative mind
• Discover the possibilities ...
Functional programming
is a programming paradigm
that describes computation as the
evaluation of mathematical functions
avoiding state and mutable data
Mario Gleichmann XPUG Rhein/Main
9. Motivation
Open your mind ...
• Functional programming for the imperative mind
• Discover the possibilities ...
Lazy Evaluation
Continuations
Monads
Recursion
Higher Order Functions
Closures
Currying
Immutable Datatypes
Mario Gleichmann XPUG Rhein/Main
10. Motivation
Open your mind ...
• Discover the (new) possibilities ...
Control Structure Abstraction
Composition Traits
Pattern Matching Type Variance
Modularity
Type Extentions / Conversions
Mario Gleichmann XPUG Rhein/Main
11. Motivation
Open your mind !!!
•
“Scala taught me to program and reason about
programming differently. I stopped thinking in terms of
allocating buffers, structs and objects and of changing those
pieces in memory. Instead I learned to think about most of
my programs as transforming input to output.
This change in thinking has lead to lower defect rates,
more modular code, and more testable code“
David Pollak
Mario Gleichmann XPUG Rhein/Main
12. What is Scala ?
A programming language ...
• Pure Object Oriented
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
13. What is Scala ?
A programming language ...
• Pure Object Oriented
• Martin Odersky (EPFL Switzerland)
• Functional
•
Pizza, Fummel & Co.
Statically Typed Java
Generic
•
•
• javac Reference Compiler
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
14. What is Scala ?
A programming language ...
''Everything is an Object''
• Pure Object Oriented
1 + 2 <=> 1.+( 2 )
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
15. What is Scala ?
A programming language ...
''Everything is an Object''
• Pure Object Oriented
1 + 2 <=> 1.+( 2 )
• Statically Typed
''No primitive Types''
123.hashCode
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
16. What is Scala ?
A programming language ...
''Everything is an Object''
• Pure Object Oriented
1 + 2 <=> 1.+( 2 )
• Statically Typed
''No primitive Types''
123.hashCode
• Functional
''Operations are method calls''
• Runs on the JVM actor ! msg <=> actor.!( msg )
Mario Gleichmann XPUG Rhein/Main
17. What is Scala ?
A programming language ...
• Pure Object Oriented
•
public BigInteger factorial( BigInteger n ){
Functional
Jav
a
if( n.equals( BigInteger.ZERO )
return BigInteger.ONE
else
• Statically Typed
return
n.multiply(
factorial( n.subtract( BigInteger.ONE ) ) );
• Runs on the JVM
}
Mario Gleichmann XPUG Rhein/Main
18. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
Scal
a
def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 )
• Statically Typed
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
19. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functionalintroduction
Method Result Type Scal
a
def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 )
• Statically Typed
Parameter list Type declaration Definition
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
20. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
Scal
a
def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 )
• Statically Typed
● (almost) everything is an expression
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
21. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
Scal
a
def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 )
• Statically Typed
● (almost) everything is an expression
BigInt 'integrates' like a Built-In type (but it's not!)
●
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
22. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
23. What is Scala ?
A programming language ...
• Pure Object Oriented
Introduction of class definition
• Functional val bigInteger: BigInteger ) extends java.lang.Number{
class BigInt(
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
24. What is Scala ?
A programming language ...
class value parameter
• Pure Object Oriented (primary constructor)
• Functional val bigInteger: BigInteger ) extends java.lang.Number{
class BigInt(
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
25. What is Scala ?
A programming language ...
• Pure Object Oriented Single Inheritance
• Functional val bigInteger: BigInteger ) extends java.lang.Number{
class BigInt(
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
26. What is Scala ?
A programming language ...
• Pure Object Oriented
Mandatory !
• Functional
class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
27. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs ordinary Method
on the JVM
Mario Gleichmann XPUG Rhein/Main
28. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
RunsClass Instantiation
on the JVM
Mario Gleichmann XPUG Rhein/Main
29. What is Scala ?
A programming language ...
• Pure Object Oriented
• Functional
class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{
override def hashCode(): Int = this.bigInteger.hashCode()
• Statically Typed BigInt =
def + (that: BigInt):
new BigInt( this.bigInteger.add( that.bigInteger ) )
...
• }
Runs on the JVM Self reference
Mario Gleichmann XPUG Rhein/Main
30. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
● Stack of Int values
● Immutable (Functional style)
• Functional
• Statically Typed
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
31. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
● Stack of Int values
● Immutable (Functional style)
• Functional
Empty Stack : Non Empty Stack :
[]
• Statically Typed element: Int [ 7 ]
Rest: Stack :
element: Int [ 23 ]
• Runs on the JVM Rest: Stack :
[]
Mario Gleichmann XPUG Rhein/Main
32. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
● Stack of Int values
● Immutable (Functional style)
• Functional
Empty Stack : Non Empty Stack :
[] element: Int [ 10 ]
Rest: Stack :
• Statically Typed element: Int [ 7 ]
Rest: Stack :
element: Int [ 23 ]
• Runs on the JVM Rest: Stack :
[]
Mario Gleichmann XPUG Rhein/Main
33. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
abstract class IntStack {
def push(x: Int): IntStack = new NonEmptyIntStack( x, this )
• Functional
def isEmpty: Boolean
• Statically Typed Int
def top:
def pop: IntStack
}
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
34. What is Scala ?
A programming language ...
Example: Implement your own Type
• Abstract class
Object Oriented
abstract class IntStack {
def push(x: Int): IntStack = new NonEmptyIntStack( x, this )
• Functional
def isEmpty: Boolean Abstract method
• Statically Typed Int
def top: Abstract method
def pop: IntStack Abstract method
}
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
35. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
class EmptyIntStack extends IntStack {
def isEmpty = true
• Functional
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackException
def pop
}
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
36. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented Inheritance
class EmptyIntStack extends IntStack {
def isEmpty = true
• Functional
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackException
def pop
}
Throwing a 'checked' Exception
• Runs on the JVM ... but catching is optional
Mario Gleichmann XPUG Rhein/Main
37. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
class EmptyIntStack extends IntStack {
def isEmpty = true
• Functional
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackException
def pop
}
val zeroInts = new EmptyIntStack
• Runs on the JVM
var noInts = new EmptyIntStack
Mario Gleichmann XPUG Rhein/Main
38. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
class EmptyIntStack extends IntStack {
def isEmpty = true
• Functional
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackException
def pop
}
val zeroInts = new EmptyIntStack 'Immutable' value
• Runs on the JVM
var noInts = new EmptyIntStack 'mutable' variable
Mario Gleichmann XPUG Rhein/Main
39. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
class EmptyIntStack extends IntStack {
def isEmpty = true
• Functional
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackExceptionNo need
def pop
} for multiple
val zeroInts = new EmptyIntStack Instances of
• Runs on the JVM EmptyIntStack
var noInts = new EmptyIntStack
Mario Gleichmann XPUG Rhein/Main
40. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
object EmptyIntStack extends IntStack {
• Functional def isEmpty = true
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackException
def pop
}
• Runs on the Singleton Object
●
JVM
Mario Gleichmann XPUG Rhein/Main
41. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
object EmptyIntStack extends IntStack {
• Functional def isEmpty = true
def top = throw new EmptyStackException
• Statically Typed = throw new EmptyStackException
def pop
}
val zeroInts = new EmptyIntStack
• Runs on the JVM
var noInts = new EmptyIntStack
Mario Gleichmann XPUG Rhein/Main
42. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
class NonEmptyIntStack( elem: Int, rest: IntStack )
extends IntStack {
• Functional
def isEmpty = false
def top = elem
• Statically Typed
def pop = rest
}
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
43. What is Scala ?
A programming language ...
Example: Implement your own Type
• Object Oriented
class NonEmptyIntStack( elem: Int, rest: IntStack )
extends IntStack {
• Functional
def isEmpty = false
def top = elem
• Statically Typed
def pop = rest
}
• Runs on the JVM
var s = EmptyIntStack push 23 push 7 push 10
Mario Gleichmann XPUG Rhein/Main
44. What is Scala ?
A programming language ...
• Pure Object Oriented
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
45. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
46. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● Using Stack with other types than Int
● Concept of stacking elements is generic
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
47. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● Using Stack with other types than Int
● Concept of stacking elements is generic
• Statically Typed
=> Generic Types
• Functional
(Type Parameterization)
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
48. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented abstract class Stack[A] { Type Paramter
def push(x: A): Stack[A] =
• Statically Typed new NonEmptyStack[A]( x, this )
def isEmpty: Boolean
• Functional def top: A
def pop: Stack[A]
• Runs on the JVM }
Mario Gleichmann XPUG Rhein/Main
49. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented class NonEmptyStack[A](
elem: A,
rest: Stack[A] ) extends Stack[A] {
• Statically Typed
def isEmpty = false
• Functional def top = elem
def pop = rest
}
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
50. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented val s = new EmptyStack[Int]
val t = s push 1 push 2 push 3
• Statically Typed
t.pop.top => 2
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
51. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented val s = new EmptyStack[Int]
val t = s push 1 push 2 push 3
• Statically Typed
t.pop.top => 2
Parameterized Type
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
52. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented val s = new EmptyStack[Int]
val t = s push 1 push 2 push 3
• Statically Typed
t.pop.top Mandatory
=> 2
( no Raw Types ! )
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
53. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● A Stack implementation only for numbers
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
54. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● A Stack implementation only for numbers
● Restrict the upper Type to Number
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
55. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● A Stack implementation only for numbers
● Restrict the upper Type to Number
• Statically Typed
●
Upper Type Bound
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
56. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● A Stack implementation only for numbers
● Restrict the upper Type to Number
• Statically Typed
●
Upper Type Bound
• Functional abstract class Stack[A <: Number]{ ... }
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
57. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented ● A Stack implementation only for numbers
● Restrict the upper Type to Number
• Statically Typed
●
Upper Type Bound
• Functional abstract class Stack[A <: Number]{ ... }
Defined by the implementor,
• Runs on the JVM not by the user !
Mario Gleichmann XPUG Rhein/Main
58. What is Scala ?
A programming language ...
Refined Type System
• Pure Object OrientedIs Stack[String] a super type of Stack[Any] ?
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
59. What is Scala ?
A programming language ...
Refined Type System
• Pure Object OrientedIs Stack[String] a super type of Stack[Any] ?
• Statically Typed
Jav
a
List<String> sList = new ArrayList<String>();
List<Object> oList = sList;
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
60. What is Scala ?
A programming language ...
Refined Type System
• Pure Object OrientedIs Stack[String] a super type of Stack[Any] ?
• Statically Typed
Jav
a
List<String> sList = new ArrayList<String>();
List<Object> oList = sList;
• Functional
Compile Error:
''Type mismatch: cannot convert
• Runs on the JVM List<String> to List<Object>''
Mario Gleichmann XPUG Rhein/Main
61. What is Scala ?
A programming language ...
Refined Type System
• Pure Object OrientedIs Stack[String] a super type of Stack[Any] ?
• Statically Typed
Jav
a
List<String> sList = new ArrayList<String>();
List<Object> oList = sList;
• Functional
Generic Types are
• Runs on the JVM INVARIANT
Mario Gleichmann XPUG Rhein/Main
62. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Orientedbut ...
• Statically Typed
Jav
a
String[] sArray = new String[]{};
Object[] oArray = sArray;
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
63. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Orientedbut ...
• Statically Typed
Jav
a
String[] sArray = new String[]{};
Object[] oArray = sArray;
• Functional
Arrays are
• Runs on the JVM COVARIANT
Mario Gleichmann XPUG Rhein/Main
64. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Orientedbut ...
• Statically Typed
Jav
a
String[] sArray = new String[]{};
Object[] oArray = sArray;
• Functional
oArray[0] = BigDecimal.ONE;
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
65. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Orientedbut ...
• Statically Typed
Jav
a
String[] sArray = new String[]{};
Object[] oArray = sArray;
• Functional
oArray[0] = BigDecimal.ONE;
ArrayStoreException
• Runs on the JVM
at Runtime !
Mario Gleichmann XPUG Rhein/Main
66. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Scal
a
abstract class Stack[+A]{
• Statically Typed def push(x: A): Stack[A] =
new NonEmptyStack[A]( x, this )
...
• Functional }
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
67. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Covariant subtyping Scal
a
abstract class Stack[+A]{
• Statically Typed def push(x: A): Stack[A] =
new NonEmptyStack[A]( x, this )
...
• Functional }
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
68. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Covariant subtyping Scal
a
abstract class Stack[+A]{
• Statically Typed def push(x: A): Stack[A] =
new NonEmptyStack[A]( x, this )
...
• Functional }
Compile Error:
''covariant type A occurs in
• Runs on the JVM contravariant position in type A
of value x''
Mario Gleichmann XPUG Rhein/Main
69. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Scal
a
abstract class Stack[+A]{
• Statically Typed def push[B >: A](x: B): Stack[B] =
new NonEmptyStack[B](x, this)
...
• Functional }
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
70. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Scal
a
abstract class Stack[+A]{
• Statically Typed def push[B >: A](x: B): Stack[B] =
new NonEmptyStack[B](x, this)
...
• Functional }
Lower Type Bound
''Parameter B is restricted to range
• Runs on the JVM only over supertypes of type A''
Mario Gleichmann XPUG Rhein/Main
71. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Scal
a
abstract class Stack[+A]{
• Statically Typed def push[B >: A](x: B): Stack[B] =
new NonEmptyStack[B](x, this)
...
• Functional }
val s1 = new EmptyStack[Int] push 1 push 2
• Runs on the JVM val s2 = s1 push "x"
Mario Gleichmann XPUG Rhein/Main
72. What is Scala ?
A programming language ...
Refined Type System
• Pure Object Oriented Scal
a
abstract class Stack[+A]{
• Statically Typed def push[B >: A](x: B): Stack[B] =
new NonEmptyStack[B](x, this)
...
• Functional } Stack[Int]
val s1 = new EmptyStack[Int] push 1 push 2
• Runs on the JVM val s2 = s1 push "x" Stack[Any]
Mario Gleichmann XPUG Rhein/Main
73. What is Scala ?
A programming language ...
Type inference
• Pure Object Oriented val creator: String = “Odersky“
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
74. What is Scala ?
A programming language ...
Type inference
• Pure Object Oriented val creator: String = “Odersky“
val creator = “Odersky“
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
75. What is Scala ?
A programming language ...
Type inference
• Pure Object Oriented val creator: String = “Odersky“
val creator = “Odersky“
• Statically Typed
• Functional def add( a: Int, b: Int ): Int = a + b
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
76. What is Scala ?
A programming language ...
Type inference
• Pure Object Oriented val creator: String = “Odersky“
val creator = “Odersky“
• Statically Typed
• Functional def add( a: Int, b: Int ): Int = a + b
def add( a: Int, b: Int ) = a + b
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
77. What is Scala ?
A programming language ...
Implicit Type conversion
• Pure Object Oriented “Scalable Language“ - 'a'
=> Sc l ble L ngu ge
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
78. What is Scala ?
A programming language ...
Implicit Type conversion
• Pure Object Oriented “Scalable Language“ - 'a'
• Statically Typed class StringExtension( s: String ){
def -( sub: Char ) = s.replace( sub, ' ' )
}
• Functional
new StringExtension( “Scala“ ).-( 'a' )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
79. What is Scala ?
A programming language ...
Implicit Type conversion
• Pure Object Oriented “Scalable Language“ - 'a'
• Statically Typed class StringExtension( s: String ){
def -( sub: Char ) = s.replace( sub, ' ' )
}
• Functional
implicit def toStringExtension( s: String ) =
new StringExtension( s )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
80. What is Scala ?
A programming language ...
Implicit Type conversion
• Pure Object Oriented “Scalable Language“ - 'a'
Implicit conversion to StringExtension
• Statically Typed class StringExtension( s: String ){
def -( sub: Char ) = s.replace( sub, ' ' )
}
• Functional
implicit def toStringExtension( s: String ) =
new StringExtension( s )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
81. What is Scala ?
A programming language ...
• Pure Object Oriented
• Statically Typed
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
82. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
83. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed Function Literals
( x: Int ) => x + 1
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
84. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed Function Literals
( x: Int ) => x + 1 => λ x . x + 1
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
85. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed Function Literals
( x: Int ) => x + 1 => λ x . x + 1
•
Functional
Argument list Definition
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
86. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed Function Literals
( x: Int ) => x + 1 => λ x . x + 1
•
Functional val succ = ( x: Int ) => x + 1
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
87. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed Function Literals
( x: Int ) => x + 1 => λ x . x + 1
•
Functional val succ = ( x: Int ) => x + 1
succ( 7 ) => 8
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
88. What is Scala ?
A programming language ...
●
Lambda Calculus (A. Church)
• Pure Object Oriented
●
Functions are first class values
• Statically Typed Function Literals
( x: Int ) => x + 1 => λ x . x + 1
•
Functional val succ = ( x: Int ) => x + 1
succ( 7 ) => 8
• Runs on the JVM type of succ: ( Int ) => Int
Mario Gleichmann XPUG Rhein/Main
89. What is Scala ?
A programming language ...
Closures
• Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 )
var barrier = 18
• Statically Typed val minors = { ( x :Int ) => x < barrier }
val germanMinors = ages.filter( minors )
•
Functional
=> List( 2, 14, 11 )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
90. What is Scala ?
A programming language ...
Closures
• Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 )
var barrier = 18 List[Int] (Type inference)
• Statically Typed val minors = { ( x :Int ) => x < barrier }
val germanMinors = ages.filter( minors )
•
Functional
... accepting a function which
=> List( 2, 14, 11 )and results to boolean
accepts an Int
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
91. What is Scala ?
A programming language ...
Closures
• Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 )
var barrier = 18 free variable
• Statically Typed val minors = { ( x :Int ) => x < barrier }
val germanMinors = ages.filter( minors )
•
Functional
bound variable
'open term'
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
92. What is Scala ?
A programming language ...
Closures
• Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 )
var barrier = 18 capturing
• Statically Typed val minors = { ( x :Int ) => x < barrier }
val germanMinors = ages.filter( minors )
•
Functional
● bound within lexical scope of function
• Runs on the JVM
=> open term is closed
Mario Gleichmann XPUG Rhein/Main
93. What is Scala ?
A programming language ...
Closures
• Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 )
var barrier = 18
• Statically Typed val minors = { ( x :Int ) => x < barrier }
val germanMinors = ages.filter( minors )
•
Functional
barrier = 21
val usMinors = ages.filter( minors )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
94. What is Scala ?
A programming language ...
Closures
• Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 )
var barrier = 18
• Statically Typed val minors = { ( x :Int ) => x < barrier }
val germanMinors = ages.filter( minors )
'dynamic' bound
•
Functional
barrier = 21
val usMinors = ages.filter( minors )
• Runs on the JVM
=> 2, 20, 14, 19, 11
Mario Gleichmann XPUG Rhein/Main
95. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int, b: Int ) => a + b
• Statically Typed A function
... accepting two Args
•
Functional
... resulting in a value of type Int
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
96. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int, b: Int ) => a + b
• Statically Typed
type of function add: ( Int, Int ) => Int
•
Functional
'resulting in ...'
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
97. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int, b: Int ) => a + b
• Statically Typed
Quiz:
''transform into a function which is accepting
•
Functional only one single Argument after another''
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
98. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b
• Statically Typed
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
99. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b
• Statically Typed A function
... accepting one Arg
•
Functional
... resulting in another function
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
100. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b
• Statically Typed
... accepting one Arg
•
Functional
... resulting in a value of type Int
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
101. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b
• Statically Typed
type of function add: (Int) => (Int) => Int
•
Functional 'resulting in ...'
'resulting in ...'
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
102. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b
• Statically Typed val succ = add( 1 )
succ( 7 ) => 8
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
103. What is Scala ?
A programming language ...
Currying
• Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b
A function ...
val succ = add( 1 ) ... accepting one Arg
• Statically Typed
... resulting in another function
succ( 7 ) => 8
•
Functional
... accepting one Arg
... resulting in a value of type Int
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
104. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
• Statically Typed
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
105. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
• Statically Typed
multiple parameter lists
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
106. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
• Statically Typed
multiple parameter lists
•
Functional
Signature: mult ( Int ) ( Int ) : Int
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
107. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
val double = mult( 2 ) _
• Statically Typed
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
108. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
val double = mult( 2 ) _
• Statically Typed
Partially applied 2nd Arg unapplied
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
109. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
val double = mult( 2 ) _
• Statically Typed
•
Functional Coercion into a function of
type ( Int ) => Int
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
110. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented def mult( a: Int )( b: Int ) = a * b
val double = mult( 2 ) _
• Statically Typed
double( 6 ) => 12
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
111. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented val hours = ( 0 to 23 ).toList
def modulo( n: Int )( x: Int ) = ( x % n ) == 0
• Statically Typed
hours.filter( modulo( 2 ) _ )
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
112. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented val hours = ( 0 to 23 ).toList List[Int]
def modulo( n: Int )( x: Int ) = ( x % n ) == 0
• Statically Typed
hours.filter( modulo( 2 ) _ )
•
Functional
expects function of type ( Int ) => Boolean
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
113. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented val hours = ( 0 to 23 ).toList
def modulo( n: Int )( x: Int ) = ( x % n ) == 0
• Statically Typed
hours.filter( modulo( 2 ) _ )
•
Functional
curried to function of type ( Int ) => Boolean
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
114. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented val hours = ( 0 to 23 ).toList
def modulo( n: Int )( x: Int ) = ( x % n ) == 0
• Statically Typed
hours.filter( modulo( 2 ) _ )
=> List( 0, 2, 4, 6, 8, 10, 12, …, 20, 22 )
•
Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
115. What is Scala ?
A programming language ...
Curried Methods
• Pure Object Oriented val hours = ( 0 to 23 ).toList
def modulo( n: Int )( x: Int ) = ( x % n ) == 0
• Statically Typed
hours.filter( modulo( 2 ) _ )
=> List( 0, 2, 4, 6, 8, 10, 12, …, 20, 22 )
•
Functional
hours.filter( modulo( 4 ) )
• Runs on the JVM => List( 0, 2, 4, 8, 12, …, 16, 20 )
Mario Gleichmann XPUG Rhein/Main
116. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
117. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented • Everything is an Object
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
118. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented • Everything is an Object
• Functions are Objects
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
119. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented • Everything is an Object
• Functions are Objects
• Statically Typed
val succ = ( x: Int ) => x + 1
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
120. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented • Everything is an Object
• Functions are Objects
• Statically Typed
val succ = new Function1[Int, Int]{
override def apply( x: Int ) = x + 1
• Functional }
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
121. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented • Everything is an Object
• Functions are Objects
• Statically Typed
val succ = new Function1[Int, Int]{
override def apply( x: Int ) = x + 1
• Functional }
succ( 7 )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
122. What is Scala ?
A programming language ...
OO + FP Fusion
●
• Pure Object Oriented • Everything is an Object
• Functions are Objects
• Statically Typed
val succ = new Function1[Int, Int]{
override def apply( x: Int ) = x + 1
• Functional }
succ.apply( 7 )
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
123. What is Scala ?
A programming language ...
• Pure Object Oriented
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
124. What is Scala ?
A programming language ...
... so does Groovy, Clojure, JRuby ...
• Pure Object Oriented
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
125. What is Scala ?
A programming language ...
... so does Groovy, Clojure, JRuby ...
• Pure Object Oriented • Dynamically typed (MOP & Co)
• Significant Performance Overhead !
• Statically Typed
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
126. What is Scala ?
A programming language ...
... so does Groovy, Clojure, JRuby ...
• Pure Object Oriented • Dynamically typed (MOP & Co)
• Significant Performance Overhead !
• Statically Typed
•
Scala is statically typed !
• Functional
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
127. What is Scala ?
A programming language ...
... so does Groovy, Clojure, JRuby ...
• Pure Object Oriented • Dynamically typed (MOP & Co)
• Significant Performance Overhead !
• Statically Typed
•
Scala is statically typed !
• Functional
• Compiles to Bytecode
• Seamless Java Interoperability
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
128. What is Scala ?
A programming language ...
... so does Groovy, Clojure, JRuby ...
• Pure Object Oriented • Dynamically typed (MOP & Co)
• Significant Performance Overhead !
• Statically Typed
•
Scala is statically typed !
• Functional
• Compiles to Bytecode
• Seamless Java Interoperability
• Runs on the JVM • Performance on par with Java
Mario Gleichmann XPUG Rhein/Main
129. What is Scala ?
A programming language ...
“I can honestly say if someone
• Pure Object Oriented
had shown me the Programming
in Scala book ... back in 2003
• Statically Typed
I'd probably have never created
Groovy“
• Functional
James Strachan
• Runs on the JVM
Mario Gleichmann XPUG Rhein/Main
130. Characteristics
•
Expressive
•
High Level
•
Concise
•
Extensible
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
131. Characteristics
•
Expressive
case class Person( name: String, age: Int )
•
High Level var persons = List( Person( "Hans", 11 ),
Person( "Hugo", 19 ),
Person( "Helga", 16 ),
Person( "Heinz", 38 ) )
•
Concise
val (adults, minors) = persons.partition( _.age > 18 )
•
Extensible
Can you spot the intention ?
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
132. Characteristics
•
Expressive
case class Person( name: String, age: Int )
•
High Level var persons = List( Person( "Hans", 11 ),
Person( "Hugo", 19 ),
Person( "Helga", 16 ),
Person( "Heinz", 38 ) )
•
Concise
val (adults, minors) = persons.partition( _.age > 18 )
•
Extensible
''Split Persons into minors and adults by their age''
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
133. Characteristics
•
Expressive
case class Person( name: String, age: Int )
•
High Level var persons = List( Person( "Hans", 11 ),
Person( "Hugo", 19 ),
Person( "Helga", 16 ),
Results into a Tuple2[List[Person],List[Person]]
Person( "Heinz", 38 ) )
•
Concise
val (adults, minors) = persons.partition( _.age > 18 )
•
Extensible
''Split Persons into minors and adults by their age''
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
134. Characteristics
•
Expressive
case class Person( name: String, age: Int )
•
High Level var persons = List( Person( "Hans", 11 ),
Person( "Hugo", 19 ),
Person( "Helga", 16 ),
Results into a Tuple2[List[Person],List[Person]]
Person( "Heinz", 38 ) )
•
Concise
val (adults, minors) = persons.partition( _.age > 18 )
•
Extensible Pattern Matching:
''Split Persons into minors and adults by their age''
bound to single elements of a Tuple2
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
135. Characteristics
•
Expressive
case class Person( name: String, age: Int )
•
High Level var persons = List( Person( "Hans", 11 ),
Person( "Hugo", 19 ),
Person( "Helga", 16 ),
Person( "Heinz", 38 ) )
•
Concise
val (adults, minors) = persons.partition( _.age > 18 )
•
Extensible
''Split Persons into minors and adults by their age''
adults => List(Person(Hugo,19), Person(Heinz,38))
•
Pragmatic minors => List(Person(Hans,11), Person(Helga,16))
Mario Gleichmann XPUG Rhein/Main
142. Characteristics
•
Expressive
•
High Level
•
Concise
•
Extensible
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
143. Characteristics
Upper Case in given name ? Jav
•
Expressive
a
boolean hasUpperCase = false;
•
High Level for( int i=0; i < name.length; i++ ){
if( Character.isUpperCase( name.charAt( i ) {
•
Concise hasUpperCase = true;
break;
}
•
Extensible
}
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
144. Characteristics
Upper Case in given name ? Scal
•
Expressive
a
val hasUpperCase =
name.exists( c: Char => c.isUpperCase )
•
High Level
•
Concise
•
Extensible
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
145. Characteristics
Upper Case in given name ? Scal
•
Expressive
a
val hasUpperCase =
name.exists( c: Char => c.isUpperCase )
•
High Level
•
Concise 'Higher Order Method' Function
•
Extensible
•
Pragmatic
Mario Gleichmann XPUG Rhein/Main
146. Characteristics
Upper Case in given name ? Scal
•
Expressive
a
val hasUpperCase =
name.exists( c: Char => c.isUpperCase )
•
High Level
•
Concise 'Higher Order Method' Function
val hasUpperCase =
•
Extensible
name.exists( c => c isUpperCase )
•
Pragmatic Type inference
Mario Gleichmann XPUG Rhein/Main
147. Characteristics
Upper Case in given name ? Scal
•
Expressive
a
val hasUpperCase =
name.exists( c: Char => c.isUpperCase )
•
High Level
•
Concise 'Higher Order Method' Function
val hasUpperCase =
•
Extensible
name.exists( _ isUpperCase )
•
Pragmatic parameter shortcut
Mario Gleichmann XPUG Rhein/Main