Sunday, June 26, 2011

Scala notes

In Scala a function value is an object. Function types are
classes that can be inherited by subclasses
. This might seem nothing more
than an academic nicety, but it has deep consequences for scalability. In fact
the actor concept could not have been implemented without
this unification of functions and objects.

Scala is more advanced than most other languages when it comes to composing
objects. An example is Scala’s traits. Traits are like interfaces in Java,
but they can also have method implementations and even fields. Objects are
constructed by mixin composition, which takes the members of a class and
adds the members of a number of traits to them. In this way, different aspects
of classes can be encapsulated in different traits. This looks a bit like
multiple inheritance, but differs when it comes to the details. Unlike a class,
a trait can add some new functionality to an unspecified superclass. This
makes traits more “pluggable” than classes. In particular, it avoids the classical
“diamond inheritance” problems of multiple inheritance, which arise
when the same class is inherited via several different paths.

Functional programming is guided by two main ideas. The first idea is
that functions are first-class values.
In a functional language, a function is a
value of the same status as, say, an integer or a string.
The second main idea of functional programming is that the operations
of a program should map input values to output values rather than change
data in place
. Another way of expressing this is that strings are immutable in Java whereas they are mutable in Ruby. So looking at just strings, Java is a functional
language, whereas Ruby is not. Immutable data structures are one
of the cornerstones of functional programming.

No comments: