Question

To quote from Chapter 1 of Programming in Scala:

... 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 shown previously could not have been implemented without this unification of functions and objects.

I don't quite understand why the author said actor concept cannot be implemented without unifying OO and functional programming. Could someone explain?

As an example, here is a simple Scala actor implementing a checksum calculator service:

actor { 
    var sum = 0
    loop {
      receive {
        case Data(bytes)       => sum += hash(bytes)
        case GetSum(requester) => requester ! sum
      }
    }
  }
Was it helpful?

Solution

"I don't quite understand why the author said actor concept cannot be implemented without unifying OO and functional programming."

The authors did not say "Functional Programming" in that quote (although Scala does take a number of such concepts as the chapter address later on), but rather..

.. In fact the actor concept shown previously could not have been implemented without this unification of functions and objects.

The major implication of this is that functions are First-Class and as such can be passed around as any other type of value - this in turn makes functions suitable for callbacks, such as "reactions".

However, the example shown really does not show "unification of functions and objects" well at all, even if they are unified underneath (even the pattern matching yields a function). As such, the quote should be taken more as a bait on a fishing line..

[Scala is a] language that grows on you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top