Scala
is an object-oriented, statically-typed and functional programming language for the JVM. The strength of Scala lies in it’s powerful syntax which allows to write readable, manageable code which is short and concise – and it comes with great support for common design pattern (i.e. a special keyword for implementing the singleton design pattern).
There are plenty of good tutorials and introductions for learning Scala in the web, but only a few mention the great possibilities programmers have to extend Scala or even build an own domain specific language using Scala without being forced to use macros as seen in LISP-like languages.
One interesting feature of Scala are “blocks” which work quite different then code blocks in Java and C++ although they use the same syntax. A block in Scala is rather a sequence of expressions which is evaluated successively and it returns the value of the last expression. Just think of them as the “do” function in LISP. Blocks are surrounded by curly braces and the expressions they hold are separated by semicolons.
| 1 2 |
scala> { val x = 2 ; x + 5 } res 0 : Int = 7 |
Thanks to some syntactic sugar the semicolon can be replaced by a line break which becomes handy in larger blocks.
| 1 2 3 4 5 |
scala> { val x = 5 x + 5 } res 1 : Int = 10 |
Because blocks are just expressions they can be used, of course, like every other expression. They can be stored in variables, returned by functions and also be used as parameters for functions:
| 1 2 3 4 5 6 7 8 |
scala> println( 2 + 5 ) 7 |