Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have record update syntax for making modified clones of immutable data structures?

In Mercury I can use:

A = B^some_field := SomeValue

to bind A to a copy of B, except that some_field is SomeValue instead of whatever it was in B. I believe the Haskell equivalent is something like:

a = b { some_field = some_value }

Does Scala have something like this for "modifying" immutable values. The alternative seems to be to have a constructor that directly sets every field in the instance, which isn't always ideal (if there are invarients the constructor should be maintaining). Plus it would be really clunky and much more fragile if I had to explicitly pass every other value in the instance I want to have a modified copy of.

I couldn't find anything about this by googling, or in a brief survey of the language reference manual or "Scala By Example" (which I have read start-to-finish, but haven't absorbed all of yet, so it may well be in there).

I can see that this feature could have some weird interactions with Java-style access protection and subclasses though...

like image 493
Ben Avatar asked Jul 12 '11 09:07

Ben


People also ask

What is the difference between immutable and mutable collection in Scala?

The section on the Immutable Collection obviously presents step-by-step examples in using Scala’s immutable data structures. Likewise, the part on the Mutable Collection naturally introduces Scala’s mutable data structures one step at a time.

Can a collection change after it is created in Scala?

Such a collection will never change after it is created. Therefore, you can rely on the fact that accessing the same collection value repeatedly at different points in time will always yield a collection with the same elements. A collection in package scala.collection.mutable is known to have some operations that change the collection in place.

What is the mutability of a Scala variant?

Each variant has different characteristics with respect to mutability. A collection in package scala.collection.immutable is guaranteed to be immutable for everyone. Such a collection will never change after it is created.

What are the different types of data structures in Scala?

This review is quite primordial so as you can easily understand the characteristics of particular data structures from either, the Immutable, or the Mutable, collection. For instance, Scala does not limit itself to providing the basic data structures, such as, a List, a Set, or a Map.


1 Answers

If you define your class as a case class, a convenient copy method is generated, and calling it you can specify with named parameters new values for certain fields.

scala> case class Sample(str: String, int: Int)
defined class Sample

scala> val s = Sample("text", 42)
s: Sample = Sample(text,42)

scala> val s2 = s.copy(str = "newText")
s2: Sample = Sample(newText,42)

It even works with polymorphic case classes:

scala> case class Sample[T](t: T, int: Int)
defined class Sample

scala> val s = Sample("text", 42)
s: Sample[java.lang.String] = Sample(text,42)

scala> val s2 = s.copy(t = List(1,2,3), 42)
s2: Sample[List[Int]] = Sample(List(1, 2, 3),42)

Note that s2 has a different type than s.

like image 153
Jean-Philippe Pellet Avatar answered Sep 23 '22 07:09

Jean-Philippe Pellet