Introduction To Scala
Introduction To Scala
Introduction To Scala
Why Scala?
scalac - compiles fsc - compiles faster! (uses a background server to minimize startup time) Go to scala-lang.org for downloads/documentation Read Scala: A Scalable Language (see http://www.artima.com/scalazine/articles/scalablelanguage.html )
4
Features of Scala
Scala is both functional and object-oriented
every value is an object every function is a value--including methods includes a local type inference system: in Java 1.5:
Pair<Integer, String> p = new Pair<Integer, String>(1, "Scala");
in Scala:
val p = new MyPair(1, "scala");
unnamed0: Int = 3 This line includes: an automatically assigned or user-defined name to refer to the computed value (unnamed0) a colon (:) the type of the expression and its resulting value (Int) an equals sign (=) the value resulting from evaluating the expression (3)
6
More features
Supports lightweight syntax for anonymous functions, higher-order functions, nested functions, currying ML-style pattern matching Integration with XML
can write XML directly in Scala program can convert XML DTD into Scala class definitions
Other features
Allows defining new control structures without using macros, and while maintaining static typing Any function can be used as an infix or postfix operator Can define methods named +, <= or ::
8
Note: var/val
Basic Scala
Use var to declare variables:
var x = 3; x += 4;
Type annotations:
var x : Int = 3;
13
Basic Scala
Class instances
val c = new IntCounter[String];
Defining functions:
def foo(x : Int) { println(x == 42); } def bar(y : Int): Int = y + 42; // no braces // needed! def return42 = 42; // No parameters either!
14
Traits
Similar to interfaces in Java They may have implementations of methods But cant contain state Can be multiply inherited from
17
More on Traits
Halfway between an interface and a class, called a trait. A class can incorporate as multiple Traits like Java interfaces but unlike interfaces they can also contain behavior, like classes. Also, like both classes and interfaces, traits can introduce new methods. Unlike either, the definition of that behavior isn't checked until the trait is actually incorporated as part of a class.
Example of traits
trait Similarity { def isSimilar(x: Any): Boolean; def isNotSimilar(x: Any): Boolean = !isSimilar(x); } class Point(xc: Int, yc: Int) with Similarity { var x: Int = xc; var y: Int = yc; def isSimilar(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x; }
19
}
20
ColoredPoint2D
Point3D
ColoredPoint3D ColoredPoint2D
class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) { val z = zc; // code for manipulating Point3Ds }
class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String) extends Point3D(xc, yc, zc) with ColoredPoint2D(xc, yc, col);
Why?
22
23
Views
Defines a coercion from one type to another Similar to conversion operators in C++/C#
trait Set { def include(x: int): Set; def contains(x: int): boolean } def view(list: List) : Set = new Set { def include(x: int): Set = x prepend xs; def contains(x: int): boolean = !isEmpty && (list.head == x || list.tail contains x) }
24
Views
Views are inserted automatically by the Scala compiler If e is of type T then a view is applied to e if:
expected type of e is not T (or a supertype) a member selected from e is not a member of T
Compiler uses only views in scope Suppose xs : List and view above is in scope
val s: Set = xs; xs contains x val s: Set = view(xs); view(xs); view(xs) view(xs) contains x
26
Compound types
In Java, the solution is:
interface CloneableAndResetable extends Cloneable, Resetable
But if the original object did not use the CloneableAndResetable interface, it wont work Scala solution: use compound types (also called intersection types)
Variance annotations
class Array[a] { def get(index: int): a def set(index: int, elem: a): unit; }
Variance Annotations
Covariance is ok with functional data structures
trait GenList[+T] { def isEmpty: boolean; def head: T; def tail: GenList[T] } object Empty extends GenList[All] { def isEmpty: boolean = true; def head: All = throw new Error("Empty.head"); def tail: List[All] = throw new Error("Empty.tail"); } class Cons[+T](x: T, xs: GenList[T]) extends GenList[T] { def isEmpty: boolean = false; def head: T = x; def tail: GenList[T] = xs
}
29
Variance Annotations
Can also have contravariant type parameters
Useful for an object that can only be written to
Types as members
abstract class AbsCell { type T; val init: T; private var value: T = init; def get: T = value; def set(x: T): unit = { value = x } } def createCell : AbsCell { new AbsCell { type T = int; val init = 1 } }
Clients of createCell cannot rely on the fact that T is int, since this information is hidden from them
Resources
The Scala programming language home page (see http://www.scala-lang.org/ ) The Scala mailing list (see http://listes.epfl.ch/cgibin/doc_en?liste=scala ) The Scala wiki (see http://scala.sygneca.com/ ) A Scala plug-in for Eclipse (see http://www.scalalang.org/downloads/eclipse/index.html ) A Scala plug-in for IntelliJ (see http://plugins.intellij.net/plugin/?id=1347 )
32
References
The Scala Programming Language as presented by Donna Malayeri (see http://www.cs.cmu.edu/~aldrich/courses/819/slides/scala.ppt ) The Scala Language Specification 2.7 (seehttp://www.scala-lang.org/docu/files/ScalaReference.pdf ) The busy Java developer's guide to Scala: Of traits and behaviorsUsing Scala's version of Java interfaces(see http://www.ibm.com/developerworks/java/library/jscala04298.html ) First Steps to Scala (in Scalazine) by Bill Venners, Martin Odersky, and Lex Spoon, May 9, 2007 (see http://www.artima.com/scalazine/articles/steps.html )
33