Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

First Introduction to Scala

A brief tour of the major features of the next big JVM language

Alan Raison

on 29 January 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of First Introduction to Scala

Scala What is Scala? Object Oriented
Strongly, Statically typed
Compiled to JVM bytecode
"Scalable" Alan Raison a first introduction Start it Up! > scala Scala IDE IntelliJ val x = 4 def incr(x: Int) =
x + 1 def test = {
if (incr(x) == 5) {
} else {
} Classes class LockManager extends MyBaseClass {
var locksInUse = 0
def take = {
locksInUse = locksInUse + 1
def release = {
locksInUse = locksInUse - 1
println("LockManager ready")
} (not functional style) A Java Bean public class PersonImpl implements Person {
private String firstname;
private String surname;
public PersonImpl(String firstname, String surname) {
this.firstname = firstname;
this.surname = surname;
public String getFirstname() {
return this.firstname;
public void setFirstname(String firstname) {
this.firstname = firstname;
public String getSurname() {
return this.surname;
public void setSurname(String surname) {
this.surname = surname;
} A Scala "Bean" class Person(firstname: String, surname: String) Case Classes A class definition with some added extras:
Generated toString, equals, hashcode methods
Generated companion object
Can be used in match expressions case class Person(firstname: String, surname: String) (try it!) val alan = new Person("Alan", "Raison") Companion Objects Singleton objects, often providing factory or utility methods to the companion class object Person {
def teenager = // construct a teenager
def adult = // construct an adult
} like Java's class variables and methods (some) core language elements Traits Feature-rich Interfaces Question: What can Java interfaces contain? public interface MyInterface extends Superinterface1, Superinterface2 {
static String CONSTANT = "1234567890";
void providesBehaviour(String name, Object argument);
} What if they could have default implementations? <T> int compareTo(T other) {
//return <0 if other is less than this
//return 0 if equal
//return >0 if other is more
} trait ComparableTrait[T] {
def compare(other: T): Int } def >=(that: T): Boolean = this > that || this == that def <=(other: T): Boolean = this.<(other) || this.==(other) def ==(other: T): Boolean = compare(other) == 0
def <(other: T): Boolean = compare(other) < 0
def >(other: T): Boolean = compare(other) > 0 class Rational(numer: Int, denom: Int) extends ComparableTrait[Rational] {
def compare(other: Rational) =
(this.numer * other.denom) - (other.numer * this.denom)
} new Rational(3, 4) < new Rational(2, 3)
new Rational(2,3) == new Rational(4,6) Pattern Matching Case classes can be used in match expressions: case class Person(firstname: String, surname: String)

e match {
case Person("Alan", _) => println("What a great name!")
case Person("Philip", surname) => println("Hi Phillys " + surname)
case Person(first, sur) => println("Hi " + first + " " + sur)
case _ => println("Not a person")
} Options Used to distinguish between Something and Nothing! abstract class Option[T]
case class Some[T](val: T)
case object None def mightReturnInt(): Option[Int]

mightReturnInt() match {
case Some(i) => println("found " + i)
case None => println("nothing")
} No NullPointerExceptions here! Integrating With Java Since it's a JVM language, it's easy! import javax.swing.{JButton, WindowConstants, JFrame}

object Application {
def main() {
val frame = new JFrame("Swing in Scala!")
frame.add(new JButton("Button"))
} An Example var y = 5
y = y + x
// x = 3 : not possible A Car Showroom Create a Car class with suitable properties
Create some single object instances of the class Create a "CarShowroom" object,
provide a method to list all Cars Create methods which filter the list to certain criteria Function Literals Functions can be passed as arguments, or assigned to variables (x: Class1, y: Class2, ...) => expression (x: Int, y: Int) => x + y List.apply("One", "Two", "Three") // or simply
List("One", "Two", "Three") case class Car(make: String, model: String, doors: Int) object CarShowroom {
def allCars = List(HondaCivic, FordFocus, PorscheBoxter)

def minDoors(number: Int) = allCars.filter( c =>
c.doors >= number) Created by Martin Odersky
Also responsible for current javac,
Java Generics
Created to make Functional Programming practical
Now owned by Typesafe who also develop Akka and the Play! Framework
Full transcript