Вопрос

I've been experimenting with Scala for some time, and have often encountered the advice to favor immutable data structures.

But when you have a data structure like e.g. a 3D scene graph, a large neural network, or anything with quite a few objects that need frequent updates (animating the objects in the scene, training the neural net, ...), this seems to be

  1. horribly inefficient at runtime since you need to constantly recreate the whole object graph, and

  2. difficult to program since when you have a reference to some objects that need to be updated, you can't just call setters on them but you need copy the object graph and replace the old objects with the updated ones.

How are such things dealt with in idiomatic Scala?

Это было полезно?

Решение

Scala is multi-paradigm: OO and functional, mutable and immutable.

Complex graphs are one example of a data structure that, as you have identified, may be easier to work with in a mutable context. If so, make the data structure mutable.

Idiomatic Scala is to use the right paradigm to solve your problem.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top