]> hello

Scala Definitions

trait A  
 extends B 
    with C1 ... with Cn {
 ...
}
(monomorphic) trait
  • like abstract class, but no variables, no side-effects.
  • if present "super-class" B must be nullary constructor
    • if present, "mixin classes" Cj must be traits
    • if present, B must be subtype of Cj's supertype for all j
abstract class A
  extends B
    with C1 ... with Cn {
 ...
}
(monomorphic) class (with nullary constructor)
  • if B is missing, then scala.AnyRef is assumed
  • if present, B must be subtype of Cj's supertype for all j
  • abstract obligatory if body contains abstract definition
class A(a1:T1,...,an:Tn*) ... 
class with n-ary constructor (rest as above)
case class A(a1:T1,...,an:Tn*) ... 
case class with n-ary constructor (rest as above)
  • Access methods aJ are automatically defined
  • an instance can be matched using pattern A(p1,...,pN)
  • toString(), hashCode() and equals(other: Any) methods are automatically added
class A[X,Y] ... 
polymorphic class (== with type parameters) (rest as above)
  • Type variables X,Y can be used like a type in constructor arguments and body.
  • also traits, case classes may be polymorphic
object A extends B {
  ...
}
object definition
  • like Java class where every definition is static
override def m(a1: T1,...,an: Tn*): T = {
  ...
}
method definition
  • override obligatory if m defined in supertype
  • if type T missing, it is inferred from body
  • if * is present, then the last argument is iterated. This means m can be called with 0,1, or more arguments of type Tn, and aN has type Seq[Tn]
  • To pass e:Seq[Tn] directly, one writes m(...,e:_*)
def m(a1: T1,...,an: Tn): T;
abstract method definition
def m[X,Y](a1: T1,...,an: Tn) ...
polymorphic method definition
  • Type variables X,Y can be used like a type in arguments and body.
override val x : T = e;
value definition
  • override obligatory if x defined in supertype
  • if type T missing, it is inferred from body
val x: T;
abstract value definition
var x : T = e;
variable definition, type T inferred if missing




Scala Constructs

new Foo(...)
new instance of Foo
if (cond) { e1 } else { e2 }
if-then-else
  • if you omit else { e2 }, then the type is assumend scala.Unit, else it is an upper bound of e1 and e2.
for(val x: T <- e1) yield { e2 }
for-comprehension, type T might be inferred from e1
  • assigns every element in Iterator or Seq e1 to x before evaluating e2
  • if yield is present then results are collected and a list returned, otherwise the type is Lscala.Unit.
while (cond) { ... };
do {...} while (cond);
x = e;
The usual stuff. Assignment works only for variables.
e match {
  case p1 => e1
     . . . 
  case pN => eN
  case _  => eElse
}
      
Pattern match.
  • if e is missing, this is used.
  • Evaluates to eJ for the first pJ that matches e.
  • The eJ may be empty! It then has type scala.Unit.
  • If nothing matches, evaluates to eElse, or if that case is missing, throws a MatchError exception.
try {
  e
} catch {
  case p1 => e1
     . . . 
  case pN => eN
  case _  => eElse
}
try-catch
  • if an exception is thrown during evaluation of e, it is matched against the patterns in catch block.

Patterns