I am new in scala and actor world.
This is what I've learned so far:
Scala is functional programming (not purely) and people advice not to use mutable state in scala.
But on another hand, there is akka framework which implements actor model. And during playing with akka I realized that the messages are immutable, BUT the state of the actors in MUTABLE. So, I need to specify inside of actors "var" variables and mutable collections. And, for me, to have a mutable state is not natural for scala.
So, does my understanding correct? Why do people adopted mutable actors model in immutable scala language?
Immutable objects and data structures are first-class citizens in Scala. This is because they prevent mistakes in distributed systems and provide thread-safe data.
In Scala, all number types, strings, and tuples are immutable. The classes Point, Date, Student, and Card we defined above are all immutable. In other words, once a Point object has been created, its fields cannot be modified.
In pure functional programming, only immutable values are used. In Scala this means: Only immutable collections classes are used, such as List, Vector, and the immutable Map and Set classes Using only immutable variables raises an interesting question: If everything is immutable, how does anything ever change?
Actors (Scala) The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.
Using only immutable variables raises an interesting question: If everything is immutable, how does anything ever change? When it comes to using collections, one answer is that you don’t mutate an existing collection; instead, you apply a function to an existing collection to create a new collection.
Apart from these the recommended approach is to use Scala case classes which are immutable (if you don’t explicitly expose the state) and works great with pattern matching at the receiver side. Other good messages types are scala.Tuple2, scala.List, scala.Map which are all immutable and great for pattern matching.
This is an interesting point and indeed actors are quite different from the rest of Scala-like code.
You can think of actors as a replacement for threads with locks, semaphores and the rest. It is a more intuitive and performant model of concurrent programming since mutable state is always encapsulated in an actor that processes messages sequentially.
It provides a solid bedrock to build more functional abstractions. Akka team has produced akka-streams. Spark is using akka to communicate across nodes. Future
s, another successful functional alternative to the problem is essentially a special case of streams: a stream of exactly one element and could be implemented using actors.
So, people use actors because some problems are solved more intuitively by having actors messaging each other than with operations on streams. Since the main argument against mutability is the complete mess it creates on concurrency, and actors solve exactly this, I guess it is okay to have this mutability. They are rather complex, but the proper solution to concurrency is actually this complex.
And just a note, streams are arguably the functional way to model any interaction of a program with its environment. E.g. for GUI development, you could provide all triggered events of controls as a stream, and folding the stream is essentially creating a way to manage the state of the program.
Scala is a language that allows mixed models. It is trues that most examples use mutable state inside Actors but you can use Actors with immutable state using become
:
class ListActor extends Actor {
def receive = changeState(List.empty[String])
def changeState(state: List[String]): Receive = {
case Add(item) =>
context.become(changeState(item :: state))
}
}
That's a good question with an interesting answer (IMHO): Scala is not functional. It's post-functional. Which means it unifies object oriented and functional programming into a single paradigm.
Within this framework, Scala favors immutability, but does not enforce it. The Actor model is one place where mutability makes sense, as all changes are local, the coordination of the concurrent behavior is done for you by the actor system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With