Scala Introduction: Saiful Bahri
Scala Introduction: Saiful Bahri
Saiful Bahri
SCALABLE LANGUAGE
➤ a Modern multi-paradigm programming language
➤ Object oriented and functional
➤ Statically typed - advanced type system
➤ Compiled to JVM bytecode
but also to CLR, and to JavaScript
➤ Very good interoperability with Java
➤ Concise, elegant, and type-safe way
➤ Can do Concurrent & Synchronize processing
HISTORY
1995
Sun and Netscape team up
and announce first beta
release of Java
HISTORY
1995
Philip Wadler tells Martin
Odersky about Java.
1999
Sun buys Java compiler from
Martin Odersky, it is
shipped with JDK 1.3
HISTORY
1999
Work on adding generics to
Java begins
HISTORY
2001
Martin Odersky starts
working on Scala at
Switzerland
AND THEN
2003 Scala 1.0
2006 Scala 2.0
2010 Scala 2.8
2011 Received a $3
million
“If I were to pick a
language to use today
other than Java, it
would be Scala”
James Gosling
NOW
➤ Open source projects in
Scala:
Akka, Lift, Spark, Kafka, Scalajs,
playframework, linkerd,
openwhisk, scio, …
https://github.com/trending/scala
➤ Tools:
built-in REPL, Eclipse, IntelliJ
and NetBeans plug-ins
integration modules for almost
every popular Java framework
http://scala-ide.org/
NOW
➤ Used in industry:
LinkedIn, Twitter, Spotify,
Foursquare, SAP
Novell, EDF, Coursera, IBM
The Guardian, Xebia, Xerox,
Sony, Siemens, …
INFLUENCES OF SCALA DESIGN
➤ Java, C#
syntax, basic types, and class libraries,
➤ Smalltalk
uniform object model,
➤ Eiffel
uniform access principle,
➤ Beta
systematic nesting,
➤ ML, Haskell
many of the functional aspects.
SCALA IS OBJECT ORIENTED
➤ Every value is object
val a = 1 + 2
val b = 1.+(2)
SCALA IS FUNCTIONAL
➤ every function is a value
scala
NO STATEMENTS
➤ Everything is an expression
➤ Return is optional, last expression is returned def plus2(x:Int)
=x+2
➤ for, while, if/else are expressions
➤ Unit equivalent of void
NO “JAVA BEAN” BOILERPLATE
Java Scala
public class Person { class Person( var name: String, var age: Int)
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(age: int) {
this.age = age;
}
}
CASE CLASS
➤ Also generates equals, hashCode and allows decomposition
via pattern matching
n match {
case it if 0 until 5 contains it => "less than five"
case it if 5 until 10 contains it => "less than ten"
case _ => "a lot“
}
CLOSURES
LIST
for {
att <- attendees
if att.name == “Fred”
lang <- att.spokenLanguages
if lang == “Danish”
} yield att
NO INTERFACES
➤ Traits for mixin composition
TRAITS FOR MULTIPLE INHERITANCE