Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala immutable vs mutable. What is the way one should go?

I'm just learning to program in scala. I have some experience in functional programming, as I have in object oriented programming. My question is kind of simple, yet tricky:

Which structures should be used in Scala? Should we only stick to immutables, eg. modifing lists by iterating through it and stick a new one together, or go for mutables? What is your opinion on that, what are the performance aspects, memory related aspects, ...

I'm likely to program in a functional style, but it often expands to an insane amount of effort to do things which are easily done by using mutables. Is it situation dependent, what to use?

like image 498
smoes Avatar asked Jan 10 '13 10:01

smoes


People also ask

Is Scala mutable or immutable?

Even though the static type of such a collection provides no operations for modifying the collection, it might still be possible that the run-time type is a mutable collection which can be changed by other clients. By default, Scala always picks immutable collections.

Why is immutability important in Scala?

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.

What is immutable data in Scala?

An object whose state cannot change after it has been constructed is called immutable (unchangable). The methods of an immutable object do not modify the state of the object. In Scala, all number types, strings, and tuples are immutable.

Are classes mutable in Scala?

In Scala, we also have Array , which are directly mapped to JVM native arrays. However, Scala allows us to create mutable data structures with the update semantics of arrays. In other words, we can define classes which are “updateable” like arrays. So you can mutate your data structures in the same style as arrays.


1 Answers

Prefer immutable to mutable state. Use mutable state only where it is absolutely necessary. Some notable reasons include:

  • Performance. The standard libraries make wide use of vars and while loops, even though this is not idiomatic Scala. This should not be emulated, however, except for cases where you have profiled to determine that modifying the code to be more imperative will bring a significant performance gain.

  • I/O. I/O, or interacting with the outside world is inherently state dependent, and thus must be dealt with in a mutable manner.

This is no different than the recommended coding style found in all major languages, imperative or functional. For example, in Java it is preferable to use data objects with only private final fields. Code written in an immutable (and functional) way is inherently easier to understand because when one sees a val, they know it will never change, reducing the possible number of states any particular object or function can be in.

In many cases, it also allows automatic parallel execution, for example, collection classes in Scala all have a par function, which will return a parallel collection that automatically run the calls to functions like map or reduce in parallel.

like image 72
Alex DiCarlo Avatar answered Oct 04 '22 01:10

Alex DiCarlo