SlideShare a Scribd company logo
1 of 35
SHORT INTRODUCTION TO SCALA
INGEGNERIA DEL SOFTWARE
Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2014 – 2015
rcardin@math.unipd.it
Ingegneria del software mod. B
INTRODUCTION
 Object/functional programming language
 Based on Java Virtual Machine
 Full interoperability with Java libraries
 Pure object orientation (no primitive types)
 Every function is a value
 High order functions (lambdas)
 Promoting immutability
 Currying
 Pattern matching
 Statically typed
 Intelligent type system
 Extensible
2Riccardo Cardin
2003:
internal
use to
EPFL
2004:
version
1.0
2006:
version
2.0
2007:
the
«growth
year»
2014: version
2.11 (JVM 8)
Ingegneria del software mod. B
INTRODUCTION
 Language main features
 Read-Eval-Print-Loop (REPL) interpreter
 An interactive shell to execute statements "on the fly"
 Variables
 UML syntax -> name : type
 val x: Int = 1 + 1 Immutable
 lazy val x: Int = 1 + 1 Immutable and lazy
 var x: Int = 1 + 1 Mutable
 def x: Int = 1 + 1 Executed every time
 Very smart type inferer val x = 1 x: Int
 No semicolon
 Blocks, blocks everywhere...
3Riccardo Cardin
Ingegneria del software mod. B
INTRODUCTION
 Language main features
 Every statement is a "pure" expression
 No return statement needed!
 The if-else statement is a value
 Imperative for statement is not supported
 Use the for-yield statement instead
 Every char can be used to define names (%$#@?...)
 Full integration with every Java library
4Riccardo Cardin
val y = 4
// x has type String
val x = if (y < 3) "minor" else "greater"
// Returns an array of integers between 2 and 101
for (i <- (1 to 100)) yield i + 1
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Everything is an object
5Riccardo Cardin
Values
References (Scala and Java)
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Classes
6Riccardo Cardin
class Person(val name: String, val surname: String,
val birth: Date) {
// Main Ctor body here
// Every secondary Ctor must call the main Ctor as
// first statement (very restrictive)
def this(name: String) = this(name, "", null)
def age(): Int = { /* return the age */ }
// Override intention must be declared
override def toString = s"My name is $name $surname"
}
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Classes
 Main constructor is declared within Class name
 Ctor body is made of statements in class body
 Accessor methods are build automatically
 val immutable value, getter only
 var  mutable value, getter and setter
 def defines methods (and functions too...)
 Use equal "=" iff the method is a function
 No public class
 You can have more than a class into a .scala file
 No static methods (wait a minute...)
7Riccardo Cardin
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 ...so let’s introduce objects notation!
8Riccardo Cardin
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Object
 Native Singleton pattern implementation
 There can be only one instance of ChuckNorris
 Companion object
 An object called as an existing class
 Companion object’s methods are static method of the class
9Riccardo Cardin
object ChuckNorris extends Person("Chuck Norris") {
// This is a static method
def roundhouseKick = "Roundhouse"
}
println ChuckNorris.roundhouseKick
class Person(val name: String, val surname: String)
object Person { // Companion object
def someStupidStaticMethod = "This is a static method"
}
println Person.someStupidStaticMethod
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Object
 Companion objects implement also Factory Method
pattern natively
 Every apply method creates an instance of the class
 Syntactic sugar: Class.apply(...)  Class(...)
 It’s like a class application (...function anyone?)
10Riccardo Cardin
class Person(val name: String,
val surname: String)
object Person {
// Factory method
def apply(n: String, s: String) =
new Person(n, s)
}
// Application of the class will return a new instance
val ricky = Person("Riccardo", "Cardin") // No new operator
There can be
more than
one apply!!!!
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Object
 Used also to define the entry point of an application
 The Java way
 The Scala way
11Riccardo Cardin
object Hello extends App {
// All the code in the object’s ctor body will be
// executed as our application.
// Arguments are accessible via an ‘args’ variable,
// available in the App trait
println("Hello World!")
}
object Hello {
// Every object containing a main method could be
// Executed. There are no restriction on file name
def main(args: Array[String]) {
println("Hello World!")
}
}
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Abstract classes
 Also defines a main constructor
 It has to be called from the derived classes’ main Ctor
12Riccardo Cardin
abstract class Animal(val species: String) {
def walk // Abstract method
}
class Person(val name: String,
val surname: String)
// Calling main ctor directly in class
// definition
extends Animal("Human") {
def walk = { /* Do some stuff */ }
}
val ricky = new Person("Riccardo", "Cardin")
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits: interfaces or abstract classes?
 Like interfaces, they have not a constructor
 To implement a single trait, use extends
 Otherwise, use with (mixin)
 But, they can have methods with implementation
and attributes
 Similar (but more powerful) to default methods in Java 8
 Multiple inheritance problem
 Class linearization: use the rightmost type (more or less...)
13Riccardo Cardin
trait A { override def toString = "I’m A" }
trait B { override def toString = "I’m B" }
trait C { override def toString = "I’m C" }
class D extends A with B, C
new D().toString // Prints ‘I’m C’
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits
 Like an interface
 Like an abstract class
14Riccardo Cardin
// No implementation at all
trait Sorter {
def sort(list: List[Int]): List[Int]
}
class QuickSorter extends Sorter {
def sort(list: List[Int]) = { /* Do something */ }
}
trait AbstractDao { // No Ctor (wtf!)
var pool: Array[Datasource]
def getPool = pool // Implemented method
def save // abstract method
def delete // abstract method
}
class UserDao extends AbstractDao {
// Has to implement save and delete
}
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits
 Inside a mixin the super refers to the previuos trait
in the linearized-type
 Used to implement the Decorator pattern natively
15Riccardo Cardin
trait Employee {
// ...
def whois(): String
}
class Engineer(name: String, office: String) extends Employee
trait ProjectManager extends Employee {
abstract override def whois() {
// super refers to the previuos trait on the left
super.whois(buffer)
println("and I’am a project manager too!")
}
}
new Engineer("Riccardo","Development") with ProjectManager
Statically binded
Decorator pattern
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits and Objects
 Using the companion object of a trait, we can
implement the Abstract Factory pattern
16Riccardo Cardin
// Abstract factory
trait AbstractFactory
// Private classes’ scope is limited to the
// definition file
private class ConcreteFactory1 extends AbstractFactory { /* ... */ }
private class ConcreteFactory2 extends AbstractFactory { /* ... */ }
object AbstractFactory{
def apply(kind: String) =
kind match {
case “factory1" => new ConcreteFactory1 ()
case “factory2" => new ConcreteFactory1 () factory 2
}
}
val factory = AbstractFactory("factory1")
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Case classes and pattern matching
 Scala has a built in pattern matching mechanism
 Case class: regular class that can be pattern-matched
 Turns all ctor params into val (immutable) constants
 Generates a companion object w/ apply methods
 Generates toString, equals and hashCode methods
properly
17Riccardo Cardin
def matchTest(x: Int): String = x match {
case 1 => "one"
case _ => "many"
}
println(matchTest(3)) // Prints ‘many’
Case clauses are
evaulated from
top to bottom
trait Term
case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term
Var("x") == Var("x") // Prints true
Checks the equality
among attributes
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Case classes and pattern matching
 Native implementation of Value Object pattern
 Obviously, case classes can be used in pattern
matching...
 The match is done recursively on the structure of the type
18Riccardo Cardin
trait Term
case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term
def printTerm(term: Term) { term match {
// The clauses uses placeholders for variables.
// If we don’t need the value, we can use Var(_)
case Var(n) =>
print(“We have a variable!”)
case Fun(x, b) =>
print(“We have a function!”)
}
}
Ingegneria del software mod. B
MISCELLANEA
 Generics: more or less like Java Generics
 Subtyping of generic types is "invariant"
 ...but it can also be used in a variant (+T) o
contravariant (-T) fashion
 There are also upper type bound (T<:A) and lower
type bound (T>:A)
19Riccardo Cardin
class Node[T](elem: T)
val node = new Node(3) // Smart type inference
case class ListNode[+T](h: T, t: ListNode[T]) {
def head: T = h
def tail: ListNode[T] = t
def prepend[U >: T](elem: U): ListNode[U] =
ListNode(elem, this)
}
Ingegneria del software mod. B
MISCELLANEA
 Option[A] type
 A list with zero (None) or one element (Some[A])
 Native implementation of the Null Object Pattern
 No more NullPointerExceptions!!
 Use them with pattern matching: results garanted!
20Riccardo Cardin
trait Option[T] {
def isDefined: Boolean
def get: T // Throws an exception if None
def getOrElse(t: T): T
}
case class Some[T](elem: T) extends Option[T]
case object None extends Option[Nothing]
def negate(x: Option[Int]) = x match {
case None => throw new IllegalArgumentException
case Some(i) => -i
}
Ingegneria del software mod. B
MISCELLANEA
 Implicit classes
 Used to translate object between similar types
 -i.e, from Java types (int) to Scala type (Int)
 Native implementation of Adapter pattern!
21Riccardo Cardin
trait Log {
def warning(message: String)
def error(message: String)
}
final class Logger {
def log(level: Level, message: String) { /* ... */ }
}
implicit class LoggerToLogAdapter(logger: Logger) extends Log {
def warning(message: String) { logger.log(WARNING, message) }
def error(message: String) { logger.log(ERROR, message) }
}
// The instance of Logger is translated to an instance of Log
val log: Log = new Logger()
The main ctor is used to
resolve the conversion
Ingegneria del software mod. B
MISCELLANEA
 Tuples
 Have you ever want a simple couple of value
container, while you were programming in Java?
 Scala has a type, Tuples, that is just a tuple of
values
 Don’t abuse of it: remember that things called classes?
 Used internally by the compilator to treat function’s
arguments
 You can build tuples with «up to 22» values
22Riccardo Cardin
val things = ("a", 1, 3.5) // (java.lang.String, Int, Double)
println(things._1) // Prints ‘a’
println(things._2) // Prints 1
// Create a tuple using the ‘->’ operator
val tuple = 1 -> "a" // (Int, java.lang.String)
Ingegneria del software mod. B
FUNCTIONAL SCALA
23Riccardo Cardin
Ingegneria del software mod. B
FUNCTIONAL SCALA
 FP, a "new" programming paradigm
 Not so young, instead
 Main principles
 Functions are high order, first-class citizen
 Refer to it, pass as argument to another function…
 Pure function
 No side effects, immutability (referential transparency)
 Recursion
24Riccardo Cardin
1930
Lambda
calculus
1958
Lisp
1990
Haskell
2003
Scala
2007
Clojure
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Functions: first-class citizens
 Functions are compiled into object of an anonymous
class extending a trait FunctionX
 X represents the number of parameters
 abstract def apply(v1: T1, v2: T2, …): R
 Strategy Pattern anyone?
25Riccardo Cardin
def name (parameters-list)[: return-type]
parameters-list := name: type
Definizione
val max = (x: Int, y: Int) => if (x < y) y else x
// Compiler builds an anonimous class, extending the trait
// FunctionX, where X is the number of parameters
val anonfun2 = new Function2[Int, Int, Int] {
// Application method, as in companion objects
def apply(x: Int, y: Int): Int = if (x < y) y else x
}
assert(max(0, 1) == anonfun2(0, 1))
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Functions (a.k.a. lambdas)
 Return clause is redundant: last expression of the
function body is the value returned
26Riccardo Cardin
// Complete definition
def hello1(s: String): String = “Hello ” + s
// Inferred return type
def hello2(s: String) = “Hello ” + s
// Using function literal
def hello3 = (s: String) => “Hello ” + s
// Assigning a function to another function, inferred type
def hello4 = hello3 // copy of definition
// ...or assigning a function to a val
val hello5 = hello4 // as val (no difference, unless...)
// Assigning a function to another function, not inferred type
def hello6: String => String = hello1
// For every param it can be use the wildcard ‘_’ if it is used only
// one time in the function body
def hello: String => String = "Hello " + _
Ingegneria del software mod. B
FUNCTIONAL SCALA
27Riccardo Cardin
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Recursion
 Let’s try to define the factorial function
 Every step depends on the local variable n and on a
recursive call of the function
 Every recursion step allocates a new frame in the stack
 Simple recursion  StackOverflowError
28Riccardo Cardin
// Simple recursion
def factorial(n: Int): Int =
if (n == 0) 1 else n * factorial(n – 1)
n * f(n – 1) n * f(n – 2) n * f(n – 3) n * f(n – 4) ...stack:
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Recursion
 We need to limit the use of the stack: tail recursion
 Every step depends only on the variables stored in
the current stack frame
 Stack frame can be reused!!!
 @tailrec  Tells Scala compiler to optimize the
execution using a while loop
29Riccardo Cardin
// Tail recursion
def factorial(n: Int): Int = {
@tailrec
def factAcc(n: Int, acc: Int): Int =
if (n == 0) acc else fastAcc(n – 1, acc * n)
// Initial call
fastAcc(n)
}
We need an
accumulator
variable
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Currying
 f: (A, B) => C, than curry(f) = fc: A => (B => C)
 Used to abstract functions, separating the
“configuration” of a function from its “inputs”
 Can you see the application of the Template Method pattern?
30Riccardo Cardin
Translation of the evaluation of a function that takes multiple arguments into
a sequence of function, each with a single argument (partial application)
// Let’s generalize functions on sequence of integers
def aggregate(id: Int, op: (Int, Int) => Int)(n: Int): Int =
if (n == 0) id
else op(aggregate(neutral, op)(n-1), n)
// Factorial of integers from n to 0
def factorial = aggregate(1, _ * _)_
// Summing integers from n to 0
def sum = aggregate(0, _ + _)_
Do you know
Haskell Curry?
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Call by value / name
 Using the call by value default approach, each
function’s argument is evaluated immediatly
 Scala implements also the call by name approach
 Arguments are evatuated only when needed
 This could lead to multiple evatualtion
31Riccardo Cardin
// Call by value
def firstNonZero(a: Int, b: Int) = if (a != 0) a else b
println(firstNonZero(1, 3)) // Prints 1
def z(): Int = z() // Non terminating function
println(firstNonZero(1, z)) // Non terminating function
// Call by name
def firstNonZeroByName(a: Int, b: => Int) = if (a != 0) a else b
println(firstNonZeroByName(1, z)) // Prints 1
Try to have a look
to streams...
Ingegneria del software mod. B
FUNCTIONAL SCALA
 First order function and call by name
 What if you want to implement the Command
pattern, saving the commands’ history?
32Riccardo Cardin
object Invoker {
// Sequence of executed commands
private var history: Seq[() => Unit] = Seq.empty
// Using call by name approach, to store the function
// inside history w/out executing it
def invoke(command: => Unit) {
command
history :+= command _
}
}
Invoker.invoke(println("foo"))
Invoker.invoke {
println("bar 1")
println("bar 2")
}
// At this point, history contains to functions
Ingegneria del software mod. B
FUNCTIONAL SCALA
 And much more...
 Scala has a very good Collection framework
 Immutability and recursion
 foreach, filter, map, flatMap, foldLeft, ...
 Streams: infinite sequence of values
 Natively dependency injection mechanism
 Cake pattern, Implicit parameters, Reader monad
 Actors model
 Monads
 ...
33Riccardo Cardin
Ingegneria del software mod. B
CONCLUSIONS
34Riccardo Cardin
Ingegneria del software mod. B
RIFERIMENTI
 Functional Programming Principles in Scala (Coursera)
https://www.coursera.org/course/progfun
 Scala School! http://twitter.github.io/scala_school/
 Scala for the Impatient, Cay Horstmann, Addison-Wesley
2012 http://www.horstmann.com/scala/index.html
 The Evolution of Scala, Martin Odersky
http://www.slideshare.net/Odersky/scala-evolution
 Design Patterns in Scala https://pavelfatin.com/design-
patterns-in-scala/
35Riccardo Cardin

More Related Content

What's hot

Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)mircodotta
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java CertificationVskills
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Javahendersk
 
Managing Binary Compatibility in Scala (Scala Days 2011)
Managing Binary Compatibility in Scala (Scala Days 2011)Managing Binary Compatibility in Scala (Scala Days 2011)
Managing Binary Compatibility in Scala (Scala Days 2011)mircodotta
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Courseparveen837153
 
Introduction to-vhdl
Introduction to-vhdlIntroduction to-vhdl
Introduction to-vhdlNeeraj Gupta
 
Basics of reflection in java
Basics of reflection in javaBasics of reflection in java
Basics of reflection in javakim.mens
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Javakim.mens
 
A Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X TransformationA Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X TransformationCoen De Roover
 
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...Coen De Roover
 
ParaSail
ParaSail  ParaSail
ParaSail AdaCore
 

What's hot (20)

Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Java
 
Core java
Core javaCore java
Core java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Managing Binary Compatibility in Scala (Scala Days 2011)
Managing Binary Compatibility in Scala (Scala Days 2011)Managing Binary Compatibility in Scala (Scala Days 2011)
Managing Binary Compatibility in Scala (Scala Days 2011)
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Introduction to VHDL
Introduction to VHDLIntroduction to VHDL
Introduction to VHDL
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Introduction to-vhdl
Introduction to-vhdlIntroduction to-vhdl
Introduction to-vhdl
 
Basics of reflection in java
Basics of reflection in javaBasics of reflection in java
Basics of reflection in java
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Java
 
A Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X TransformationA Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X Transformation
 
Core java
Core javaCore java
Core java
 
Core java
Core java Core java
Core java
 
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
 
ParaSail
ParaSail  ParaSail
ParaSail
 

Viewers also liked

Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyBozhidar Bozhanov
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignRiccardo Cardin
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patternsRiccardo Cardin
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java ProgrammersEnno Runne
 
Errori comuni nei documenti di Analisi dei Requisiti
Errori comuni nei documenti di Analisi dei RequisitiErrori comuni nei documenti di Analisi dei Requisiti
Errori comuni nei documenti di Analisi dei RequisitiRiccardo Cardin
 
Introduzione ai Design Pattern
Introduzione ai Design PatternIntroduzione ai Design Pattern
Introduzione ai Design PatternRiccardo Cardin
 
Presto updates to 0.178
Presto updates to 0.178Presto updates to 0.178
Presto updates to 0.178Kai Sasaki
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics ProgrammingRiccardo Cardin
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Design Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionDesign Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionRiccardo Cardin
 
Design Pattern Strutturali
Design Pattern StrutturaliDesign Pattern Strutturali
Design Pattern StrutturaliRiccardo Cardin
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and outputRiccardo Cardin
 
Design pattern architetturali Model View Controller, MVP e MVVM
Design pattern architetturali   Model View Controller, MVP e MVVMDesign pattern architetturali   Model View Controller, MVP e MVVM
Design pattern architetturali Model View Controller, MVP e MVVMRiccardo Cardin
 

Viewers also liked (16)

Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very ugly
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented Design
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patterns
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java Programmers
 
Errori comuni nei documenti di Analisi dei Requisiti
Errori comuni nei documenti di Analisi dei RequisitiErrori comuni nei documenti di Analisi dei Requisiti
Errori comuni nei documenti di Analisi dei Requisiti
 
Introduzione ai Design Pattern
Introduzione ai Design PatternIntroduzione ai Design Pattern
Introduzione ai Design Pattern
 
Diagrammi delle Classi
Diagrammi delle ClassiDiagrammi delle Classi
Diagrammi delle Classi
 
Presto updates to 0.178
Presto updates to 0.178Presto updates to 0.178
Presto updates to 0.178
 
Diagrammi di Sequenza
Diagrammi di SequenzaDiagrammi di Sequenza
Diagrammi di Sequenza
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics Programming
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Design Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionDesign Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency Injection
 
Java - Sockets
Java - SocketsJava - Sockets
Java - Sockets
 
Design Pattern Strutturali
Design Pattern StrutturaliDesign Pattern Strutturali
Design Pattern Strutturali
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
 
Design pattern architetturali Model View Controller, MVP e MVVM
Design pattern architetturali   Model View Controller, MVP e MVVMDesign pattern architetturali   Model View Controller, MVP e MVVM
Design pattern architetturali Model View Controller, MVP e MVVM
 

Similar to A (too) Short Introduction to Scala

Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaMichael Stal
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingGarth Gilmour
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plusSayed Ahmed
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Sayed Ahmed
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.Brian Hsu
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbowschrisbuckett
 

Similar to A (too) Short Introduction to Scala (20)

Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
 

More from Riccardo Cardin

Design Pattern Comportamentali
Design Pattern ComportamentaliDesign Pattern Comportamentali
Design Pattern ComportamentaliRiccardo Cardin
 
Design Pattern Creazionali
Design Pattern CreazionaliDesign Pattern Creazionali
Design Pattern CreazionaliRiccardo Cardin
 
Mvc e di spring e angular js
Mvc e di   spring e angular jsMvc e di   spring e angular js
Mvc e di spring e angular jsRiccardo Cardin
 
Reactive programming principles
Reactive programming principlesReactive programming principles
Reactive programming principlesRiccardo Cardin
 

More from Riccardo Cardin (7)

Design Pattern Comportamentali
Design Pattern ComportamentaliDesign Pattern Comportamentali
Design Pattern Comportamentali
 
Design Pattern Creazionali
Design Pattern CreazionaliDesign Pattern Creazionali
Design Pattern Creazionali
 
Diagrammi di Attività
Diagrammi di AttivitàDiagrammi di Attività
Diagrammi di Attività
 
Diagrammi Use Case
Diagrammi Use CaseDiagrammi Use Case
Diagrammi Use Case
 
Introduzione a UML
Introduzione a UMLIntroduzione a UML
Introduzione a UML
 
Mvc e di spring e angular js
Mvc e di   spring e angular jsMvc e di   spring e angular js
Mvc e di spring e angular js
 
Reactive programming principles
Reactive programming principlesReactive programming principles
Reactive programming principles
 

Recently uploaded

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Recently uploaded (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

A (too) Short Introduction to Scala

  • 1. SHORT INTRODUCTION TO SCALA INGEGNERIA DEL SOFTWARE Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2014 – 2015 rcardin@math.unipd.it
  • 2. Ingegneria del software mod. B INTRODUCTION  Object/functional programming language  Based on Java Virtual Machine  Full interoperability with Java libraries  Pure object orientation (no primitive types)  Every function is a value  High order functions (lambdas)  Promoting immutability  Currying  Pattern matching  Statically typed  Intelligent type system  Extensible 2Riccardo Cardin 2003: internal use to EPFL 2004: version 1.0 2006: version 2.0 2007: the «growth year» 2014: version 2.11 (JVM 8)
  • 3. Ingegneria del software mod. B INTRODUCTION  Language main features  Read-Eval-Print-Loop (REPL) interpreter  An interactive shell to execute statements "on the fly"  Variables  UML syntax -> name : type  val x: Int = 1 + 1 Immutable  lazy val x: Int = 1 + 1 Immutable and lazy  var x: Int = 1 + 1 Mutable  def x: Int = 1 + 1 Executed every time  Very smart type inferer val x = 1 x: Int  No semicolon  Blocks, blocks everywhere... 3Riccardo Cardin
  • 4. Ingegneria del software mod. B INTRODUCTION  Language main features  Every statement is a "pure" expression  No return statement needed!  The if-else statement is a value  Imperative for statement is not supported  Use the for-yield statement instead  Every char can be used to define names (%$#@?...)  Full integration with every Java library 4Riccardo Cardin val y = 4 // x has type String val x = if (y < 3) "minor" else "greater" // Returns an array of integers between 2 and 101 for (i <- (1 to 100)) yield i + 1
  • 5. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Everything is an object 5Riccardo Cardin Values References (Scala and Java)
  • 6. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Classes 6Riccardo Cardin class Person(val name: String, val surname: String, val birth: Date) { // Main Ctor body here // Every secondary Ctor must call the main Ctor as // first statement (very restrictive) def this(name: String) = this(name, "", null) def age(): Int = { /* return the age */ } // Override intention must be declared override def toString = s"My name is $name $surname" }
  • 7. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Classes  Main constructor is declared within Class name  Ctor body is made of statements in class body  Accessor methods are build automatically  val immutable value, getter only  var  mutable value, getter and setter  def defines methods (and functions too...)  Use equal "=" iff the method is a function  No public class  You can have more than a class into a .scala file  No static methods (wait a minute...) 7Riccardo Cardin
  • 8. Ingegneria del software mod. B OBJECT ORIENTED SCALA  ...so let’s introduce objects notation! 8Riccardo Cardin
  • 9. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Object  Native Singleton pattern implementation  There can be only one instance of ChuckNorris  Companion object  An object called as an existing class  Companion object’s methods are static method of the class 9Riccardo Cardin object ChuckNorris extends Person("Chuck Norris") { // This is a static method def roundhouseKick = "Roundhouse" } println ChuckNorris.roundhouseKick class Person(val name: String, val surname: String) object Person { // Companion object def someStupidStaticMethod = "This is a static method" } println Person.someStupidStaticMethod
  • 10. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Object  Companion objects implement also Factory Method pattern natively  Every apply method creates an instance of the class  Syntactic sugar: Class.apply(...)  Class(...)  It’s like a class application (...function anyone?) 10Riccardo Cardin class Person(val name: String, val surname: String) object Person { // Factory method def apply(n: String, s: String) = new Person(n, s) } // Application of the class will return a new instance val ricky = Person("Riccardo", "Cardin") // No new operator There can be more than one apply!!!!
  • 11. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Object  Used also to define the entry point of an application  The Java way  The Scala way 11Riccardo Cardin object Hello extends App { // All the code in the object’s ctor body will be // executed as our application. // Arguments are accessible via an ‘args’ variable, // available in the App trait println("Hello World!") } object Hello { // Every object containing a main method could be // Executed. There are no restriction on file name def main(args: Array[String]) { println("Hello World!") } }
  • 12. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Abstract classes  Also defines a main constructor  It has to be called from the derived classes’ main Ctor 12Riccardo Cardin abstract class Animal(val species: String) { def walk // Abstract method } class Person(val name: String, val surname: String) // Calling main ctor directly in class // definition extends Animal("Human") { def walk = { /* Do some stuff */ } } val ricky = new Person("Riccardo", "Cardin")
  • 13. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits: interfaces or abstract classes?  Like interfaces, they have not a constructor  To implement a single trait, use extends  Otherwise, use with (mixin)  But, they can have methods with implementation and attributes  Similar (but more powerful) to default methods in Java 8  Multiple inheritance problem  Class linearization: use the rightmost type (more or less...) 13Riccardo Cardin trait A { override def toString = "I’m A" } trait B { override def toString = "I’m B" } trait C { override def toString = "I’m C" } class D extends A with B, C new D().toString // Prints ‘I’m C’
  • 14. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits  Like an interface  Like an abstract class 14Riccardo Cardin // No implementation at all trait Sorter { def sort(list: List[Int]): List[Int] } class QuickSorter extends Sorter { def sort(list: List[Int]) = { /* Do something */ } } trait AbstractDao { // No Ctor (wtf!) var pool: Array[Datasource] def getPool = pool // Implemented method def save // abstract method def delete // abstract method } class UserDao extends AbstractDao { // Has to implement save and delete }
  • 15. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits  Inside a mixin the super refers to the previuos trait in the linearized-type  Used to implement the Decorator pattern natively 15Riccardo Cardin trait Employee { // ... def whois(): String } class Engineer(name: String, office: String) extends Employee trait ProjectManager extends Employee { abstract override def whois() { // super refers to the previuos trait on the left super.whois(buffer) println("and I’am a project manager too!") } } new Engineer("Riccardo","Development") with ProjectManager Statically binded Decorator pattern
  • 16. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits and Objects  Using the companion object of a trait, we can implement the Abstract Factory pattern 16Riccardo Cardin // Abstract factory trait AbstractFactory // Private classes’ scope is limited to the // definition file private class ConcreteFactory1 extends AbstractFactory { /* ... */ } private class ConcreteFactory2 extends AbstractFactory { /* ... */ } object AbstractFactory{ def apply(kind: String) = kind match { case “factory1" => new ConcreteFactory1 () case “factory2" => new ConcreteFactory1 () factory 2 } } val factory = AbstractFactory("factory1")
  • 17. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Case classes and pattern matching  Scala has a built in pattern matching mechanism  Case class: regular class that can be pattern-matched  Turns all ctor params into val (immutable) constants  Generates a companion object w/ apply methods  Generates toString, equals and hashCode methods properly 17Riccardo Cardin def matchTest(x: Int): String = x match { case 1 => "one" case _ => "many" } println(matchTest(3)) // Prints ‘many’ Case clauses are evaulated from top to bottom trait Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term Var("x") == Var("x") // Prints true Checks the equality among attributes
  • 18. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Case classes and pattern matching  Native implementation of Value Object pattern  Obviously, case classes can be used in pattern matching...  The match is done recursively on the structure of the type 18Riccardo Cardin trait Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term def printTerm(term: Term) { term match { // The clauses uses placeholders for variables. // If we don’t need the value, we can use Var(_) case Var(n) => print(“We have a variable!”) case Fun(x, b) => print(“We have a function!”) } }
  • 19. Ingegneria del software mod. B MISCELLANEA  Generics: more or less like Java Generics  Subtyping of generic types is "invariant"  ...but it can also be used in a variant (+T) o contravariant (-T) fashion  There are also upper type bound (T<:A) and lower type bound (T>:A) 19Riccardo Cardin class Node[T](elem: T) val node = new Node(3) // Smart type inference case class ListNode[+T](h: T, t: ListNode[T]) { def head: T = h def tail: ListNode[T] = t def prepend[U >: T](elem: U): ListNode[U] = ListNode(elem, this) }
  • 20. Ingegneria del software mod. B MISCELLANEA  Option[A] type  A list with zero (None) or one element (Some[A])  Native implementation of the Null Object Pattern  No more NullPointerExceptions!!  Use them with pattern matching: results garanted! 20Riccardo Cardin trait Option[T] { def isDefined: Boolean def get: T // Throws an exception if None def getOrElse(t: T): T } case class Some[T](elem: T) extends Option[T] case object None extends Option[Nothing] def negate(x: Option[Int]) = x match { case None => throw new IllegalArgumentException case Some(i) => -i }
  • 21. Ingegneria del software mod. B MISCELLANEA  Implicit classes  Used to translate object between similar types  -i.e, from Java types (int) to Scala type (Int)  Native implementation of Adapter pattern! 21Riccardo Cardin trait Log { def warning(message: String) def error(message: String) } final class Logger { def log(level: Level, message: String) { /* ... */ } } implicit class LoggerToLogAdapter(logger: Logger) extends Log { def warning(message: String) { logger.log(WARNING, message) } def error(message: String) { logger.log(ERROR, message) } } // The instance of Logger is translated to an instance of Log val log: Log = new Logger() The main ctor is used to resolve the conversion
  • 22. Ingegneria del software mod. B MISCELLANEA  Tuples  Have you ever want a simple couple of value container, while you were programming in Java?  Scala has a type, Tuples, that is just a tuple of values  Don’t abuse of it: remember that things called classes?  Used internally by the compilator to treat function’s arguments  You can build tuples with «up to 22» values 22Riccardo Cardin val things = ("a", 1, 3.5) // (java.lang.String, Int, Double) println(things._1) // Prints ‘a’ println(things._2) // Prints 1 // Create a tuple using the ‘->’ operator val tuple = 1 -> "a" // (Int, java.lang.String)
  • 23. Ingegneria del software mod. B FUNCTIONAL SCALA 23Riccardo Cardin
  • 24. Ingegneria del software mod. B FUNCTIONAL SCALA  FP, a "new" programming paradigm  Not so young, instead  Main principles  Functions are high order, first-class citizen  Refer to it, pass as argument to another function…  Pure function  No side effects, immutability (referential transparency)  Recursion 24Riccardo Cardin 1930 Lambda calculus 1958 Lisp 1990 Haskell 2003 Scala 2007 Clojure
  • 25. Ingegneria del software mod. B FUNCTIONAL SCALA  Functions: first-class citizens  Functions are compiled into object of an anonymous class extending a trait FunctionX  X represents the number of parameters  abstract def apply(v1: T1, v2: T2, …): R  Strategy Pattern anyone? 25Riccardo Cardin def name (parameters-list)[: return-type] parameters-list := name: type Definizione val max = (x: Int, y: Int) => if (x < y) y else x // Compiler builds an anonimous class, extending the trait // FunctionX, where X is the number of parameters val anonfun2 = new Function2[Int, Int, Int] { // Application method, as in companion objects def apply(x: Int, y: Int): Int = if (x < y) y else x } assert(max(0, 1) == anonfun2(0, 1))
  • 26. Ingegneria del software mod. B FUNCTIONAL SCALA  Functions (a.k.a. lambdas)  Return clause is redundant: last expression of the function body is the value returned 26Riccardo Cardin // Complete definition def hello1(s: String): String = “Hello ” + s // Inferred return type def hello2(s: String) = “Hello ” + s // Using function literal def hello3 = (s: String) => “Hello ” + s // Assigning a function to another function, inferred type def hello4 = hello3 // copy of definition // ...or assigning a function to a val val hello5 = hello4 // as val (no difference, unless...) // Assigning a function to another function, not inferred type def hello6: String => String = hello1 // For every param it can be use the wildcard ‘_’ if it is used only // one time in the function body def hello: String => String = "Hello " + _
  • 27. Ingegneria del software mod. B FUNCTIONAL SCALA 27Riccardo Cardin
  • 28. Ingegneria del software mod. B FUNCTIONAL SCALA  Recursion  Let’s try to define the factorial function  Every step depends on the local variable n and on a recursive call of the function  Every recursion step allocates a new frame in the stack  Simple recursion  StackOverflowError 28Riccardo Cardin // Simple recursion def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n – 1) n * f(n – 1) n * f(n – 2) n * f(n – 3) n * f(n – 4) ...stack:
  • 29. Ingegneria del software mod. B FUNCTIONAL SCALA  Recursion  We need to limit the use of the stack: tail recursion  Every step depends only on the variables stored in the current stack frame  Stack frame can be reused!!!  @tailrec  Tells Scala compiler to optimize the execution using a while loop 29Riccardo Cardin // Tail recursion def factorial(n: Int): Int = { @tailrec def factAcc(n: Int, acc: Int): Int = if (n == 0) acc else fastAcc(n – 1, acc * n) // Initial call fastAcc(n) } We need an accumulator variable
  • 30. Ingegneria del software mod. B FUNCTIONAL SCALA  Currying  f: (A, B) => C, than curry(f) = fc: A => (B => C)  Used to abstract functions, separating the “configuration” of a function from its “inputs”  Can you see the application of the Template Method pattern? 30Riccardo Cardin Translation of the evaluation of a function that takes multiple arguments into a sequence of function, each with a single argument (partial application) // Let’s generalize functions on sequence of integers def aggregate(id: Int, op: (Int, Int) => Int)(n: Int): Int = if (n == 0) id else op(aggregate(neutral, op)(n-1), n) // Factorial of integers from n to 0 def factorial = aggregate(1, _ * _)_ // Summing integers from n to 0 def sum = aggregate(0, _ + _)_ Do you know Haskell Curry?
  • 31. Ingegneria del software mod. B FUNCTIONAL SCALA  Call by value / name  Using the call by value default approach, each function’s argument is evaluated immediatly  Scala implements also the call by name approach  Arguments are evatuated only when needed  This could lead to multiple evatualtion 31Riccardo Cardin // Call by value def firstNonZero(a: Int, b: Int) = if (a != 0) a else b println(firstNonZero(1, 3)) // Prints 1 def z(): Int = z() // Non terminating function println(firstNonZero(1, z)) // Non terminating function // Call by name def firstNonZeroByName(a: Int, b: => Int) = if (a != 0) a else b println(firstNonZeroByName(1, z)) // Prints 1 Try to have a look to streams...
  • 32. Ingegneria del software mod. B FUNCTIONAL SCALA  First order function and call by name  What if you want to implement the Command pattern, saving the commands’ history? 32Riccardo Cardin object Invoker { // Sequence of executed commands private var history: Seq[() => Unit] = Seq.empty // Using call by name approach, to store the function // inside history w/out executing it def invoke(command: => Unit) { command history :+= command _ } } Invoker.invoke(println("foo")) Invoker.invoke { println("bar 1") println("bar 2") } // At this point, history contains to functions
  • 33. Ingegneria del software mod. B FUNCTIONAL SCALA  And much more...  Scala has a very good Collection framework  Immutability and recursion  foreach, filter, map, flatMap, foldLeft, ...  Streams: infinite sequence of values  Natively dependency injection mechanism  Cake pattern, Implicit parameters, Reader monad  Actors model  Monads  ... 33Riccardo Cardin
  • 34. Ingegneria del software mod. B CONCLUSIONS 34Riccardo Cardin
  • 35. Ingegneria del software mod. B RIFERIMENTI  Functional Programming Principles in Scala (Coursera) https://www.coursera.org/course/progfun  Scala School! http://twitter.github.io/scala_school/  Scala for the Impatient, Cay Horstmann, Addison-Wesley 2012 http://www.horstmann.com/scala/index.html  The Evolution of Scala, Martin Odersky http://www.slideshare.net/Odersky/scala-evolution  Design Patterns in Scala https://pavelfatin.com/design- patterns-in-scala/ 35Riccardo Cardin