Scala meetup - Milan, 25 May 2013
Una delle caratteristiche peculiari di Scala consiste nel semplificare e velocizzare la fase di sviluppo del software. Unificando la programmazione orientata agli oggetti con la programmazione funzionale, Scala permette di esprimervi in maniera concisa ed efficace. In questa presentazione saranno introdotte le caratteristiche principali del linguaggio e la sua filosofia, al fine di mostrare come del codice scritto in Scala risulti più semplice, corretto e manutenibile.
2. Is Scala the Java of the
future?
1/38
Sunday, May 26, 13
3. • It has basically everything Java has now
• It has closures (planned for Java 8)
• It has rich interfaces (Java 8 defender
methods), and more
• It is completely interoperable and runs
about as fast as Java
2/38
Sunday, May 26, 13
4. How is Scala different
from Java?
3/38
Sunday, May 26, 13
6. public class Time {
private final int hours;
private final int minutes;
public Time(int hours, int minutes) {
this.hours = hours;
this.minutes = minutes;
}
public int getHours() { return hours; }
public int getMinutes() { return minutes; }
}
class Time(val hours: Int, val minutes: Int)
5/38
Sunday, May 26, 13
13. def max(x: Int, y: Int): Int =
if (x > y) x else y
no return statement
val x: Int = {
val y = 10
val z = 5
y + z
}
blocks evaluate to last
expression
12/38
Sunday, May 26, 13
23. • More general than for-loops
• Used to iterate, filter, and generate new
collections
21/38
Sunday, May 26, 13
24. for (p <- persons; pr <- p.projects;
if pr.overdue) yield p.name
may have any number of generators
guard construct a new collection of the same
type, element by element
p is in scope for other generators
22/38
Sunday, May 26, 13
29. • Like Java interfaces, but traits
• can have behavior (like Java 8 interfaces
with defender methods)
• can have state
• enable multiple inheritance
27/38
Sunday, May 26, 13
31. Multiple Inheritance
• Traits can mix-in multiple traits
• Classes can mix-in multiple traits
• Both Class and Trait can inherit at most
from one Class
29/38
Sunday, May 26, 13
32. trait Bird {
def fly: String = "I'm flying!"
}
trait Swimmer {
def swim: String = "I'm swimming!"
}
class Fish extends Swimmer
class Duck extends Bird with Swimmer
30/38
Sunday, May 26, 13