1. If I were to pick a language to use on the JVM
today other than Java, it would be Scala.
– James Gosling, creator of Java
http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming
Scala, it must be stated, is the current heir apparent to the
Java throne. No other language on the JVM seems as
capable of being a "replacement for Java" as Scala, and
the momentum behind Scala is now unquestionable.
– Charlies Nutter, JRuby lead
http://blog.headius.com/2009/04/future-part-one.html
My tip though for the long term replacement of javac is Scala.
I'm very impressed with it! I can honestly say if someone had
shown me the Programming in Scala book […] back in 2003
I'd probably have never created Groovy.
– James Strachan, creator of Groovy
http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html
2. Agenda
● Background and motivation
● Intro to Scala
● Basic syntax
● Pattern matching
● Functions
● Classes and traits
● <break />
● Practical Scala
3. About us
● Alf Kristian Støyle and Fredrik Vraalsen
● Java developers with 6 and 9 years experience
● Scala enthusiasts since 2008
● Developed SubmitIT for JavaZone
● Alf worked on Scala projects for
Kommuneforlaget
● Held 4 Scala training courses for 60+
participants
4.
5. public class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
class Person(var age: Int, var name: String)
6. List<Person> persons = ...
List<Person> adults = new LinkedList<Person>();
List<Person> kids = new LinkedList<Person>();
for (Person person : persons) {
if (person.getAge() < 18) {
kids.add(person);
} else {
adults.add(person);
}
}
val persons: List[Person] = ...
val (kids, adults) = persons.partition(_.age < 18)
18. Collections
val list = List("apple", "orange", "banana")
val map = Map(1 -> "one", 2 -> "two")
val array = Array(1, 2, 3, 4, 5)
list(1) // orange
map(2) // two
array(3) // 4
19. myObject match {
case 1 => println("First")
case 2 => println("Second")
case _ => println("Unknown")
}
Pattern matching
20. myObject match {
case i: Int => println("Found number " + i)
case s: String => println("Found text " + s)
case _ => println("Unknown")
}
Pattern matching
21. val email = """(.+)@(.+)""".r
"scala@java.no" match {
case email(name, domain) =>
println("User " + name + " at " + domain)
case x => println(x + " is not an email")
}
Pattern matching
31. In practice
● Only immutable objects (and object graphs)
● All field must be immutable
● No side-effects from method calls
● All methods must return something
32. Immutable datastrukturer
for (Iterator i = persons.iterator(); i.hasNext();) {
Person person = (Person) i.next();
if (person.getAge() < 18) {
i.remove();
}
}
val adults = persons.filter(_.age >= 18)
33. Strive to be pure
● Concurrency
● Easier to avoid errors
● Easier to test
34. Control structures return values
val numbers = for (i <- 1 to 10) yield i
val res1 = if (cond) x else y
val res2 = try { x } catch { … y } finally { … }
36. Classes and constructors
class Person(val name: String, val age: Int) {
def this(name: String) = this(name, 42)
override def toString = "My age is " + age
}
43. Practical Scala
● The downside
● Tools
● ScalaTest
● Java interoperability
● Learning curve
● Your choice
● Implicit conversions
● Higher order functions
44. IDE support
● Netbeans - very good, but inferior in other
areas
● IntelliJ IDEA - very good, but slow compilation
● Eclipse - getting better, very fast when working
45. Backward compatibility
● Binary compatibility broken several times
● Think twice before using a library
● 2.7.3 – 2.7.4
● 2.8.0 <-> 2.8.1.RC4
50. Learning curve
● Syntax small smack in the face
● Easy to write Java-ish Scala
● The language scales with your understanding
● Gradually migrate to more functional style
51. What would you like to hear?
● Implicit conversion
● Why does "gnirtS.gnal.avaj".reverse work?
● Higher order functions
● How does the using/resource handling example work?
57. call-by-value vs. call-by-name
● by-value: expressions are evaluated before
being passed to the function
● by-name: expressions evaluated inside function
● nice when computationally expensive
● possible to create nice APIs
76. Implicit rules!
● Marking Rule: Only definitions marked implicit are available.
● Scope Rule: An inserted implicit conversion must be in scope as a
single identifier, or be associated with the source or target type of the
conversion.
● Non-Ambiguity Rule: An implicit conversion is only inserted if there is
no other possible conversion to insert.
● One-at-a-time Rule: Only one implicit is tried.
● Explicits-First Rule: Whenever code type checks as it is written, no
implicits are attempted.