Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ======= Scala @ PP ======= ===== "Meta ideas" ===== Test-driven development ===== 1. Installation ===== ===== 2. Syntax ===== 2.1. Writing functions 2.2. Function evaluation (order of parameters in function call matters !) - using the ''=>'' to delay evaluation 2.3. Conditionals - '' def f(x : Int) = if (x >= 0) x else -x'' - boolean expressions (''true'', ''false'', ''!b'', ''&&'', ''||'') - the "by value" definition using ''val'' - example using def and val against a loop Newton square root estimation * Blocks - expressions in Scala * visibility of variables in blocks * local scoping (similar to let) * semicolons (optional) * tail recursion in Scala (only direct recursive calls are optimised @tailrec) * function types (''f : Int => Int'') * anonymous functions (''(x: Int, y: Int) => x + y'', '' x => x+x'') * currying (''def f(x: Int, y: Int)(z: Int):Int = x+y+z'') * higher-order functions (mapReduce implementation) * classes and **methods** <code scala> class Rational(x: Int, y: Int) { // creates a Type as well as a constructor for the new type def a = x def b = y } val r = new Rational(1,2) </code> * ''toString'' and ''@override'' * the ''require'' (a precondition on the caller of a function) (equivalent to) and ''assert'' (check the code is right) * **constructors**: * multiple constructors: <code scala> class ... { def this(x: Int) = this(x,1) } </code> * infix notation for functions <code scala> r add s /*same as*/ r.add(s) def < (param : Rational) /* symbolic identifiers may contain operator symbols */ def unary_- (...) /* to overload negation. Careful, white spaces can be interpreted as identifiers */ /* the precedence of an operator is determined by its first character */ /* precedence cannot be overridden by the programmer */ </code> ===== 3. Data representation ====== * object definitions - singleton classes ''object Empty extends IntSet'' * companion objects (one object per class) * annotation ''@main def birthday'' - can be called directly from compilation ''scala birthday ...'' * ''println(s"Hello $name")'' * **dynamic binding** * packages - organising sources - directories (not enforced by the fs) <code scala> import pack.Obj </code> * traits (similar to interfaces in Java, but can have parameters, contain fields and defined functions) * object hierarchy in Scala * exceptions (similar to Scala)