2

val and var in scala, the concept is understandable enough, I think.

I wanted to do something like this (java like):

trait PersonInfo {
  var name: Option[String] = None
  var address: Option[String] = None
  // plus another 30 var, for example 
 }
case class Person() extends PersonInfo
object TestObject {
  def main(args: Array[String]): Unit = {
         val p = new Person()
         p.name = Some("someName")
         p.address = Some("someAddress")
  }
}

so I can change the name, address, etc...

This works well enough, but the thing is, in my program I end up with everything as vars. As I understand val are "preferred" in scala. How can val work in this type of example without having to rewrite all 30+ arguments every time one of them is changed?

That is, I could have

trait PersonInfo {
  val name: Option[String] 
  val address: Option[String] 
  // plus another 30 val, for example 
 }
case class Person(name: Option[String]=None, address: Option[String]=None, ...plus another 30.. ) extends PersonInfo
object TestObject {
  def main(args: Array[String]): Unit = {
         val p = new Person("someName", "someAddress", .....)
         // and if I want to change one thing, the address for example
         val p2 = new Person("someName", "someOtherAddress", .....)
  }
}

Is this the "normal" scala way of doing thing (not withstanding the 22 parameters limit)? As can be seen, I'm very new to all this.


At first the basic option of Tony K.:

def withName(n : String) = Person(n, address)

looked promising, but I have quite a few classes that extends PersonInfo. That means in each one I would have to re-implement the defs, lots of typing and cutting and pasting, just to do something simple. If I convert the trait PersonInfo to a normal class and put all the defs in it, then I have the problem of how can I return a Person, not a PersonInfo? Is there a clever scala thing to somehow implement in the trait or super class and have all subclasses really extend?

As far as I can see all works very well in scala when the examples are very simple, 2 or 3 parameters, but when you have dozens it becomes very tedious and unworkable.

PersonContext of weirdcanada is I think similar, still thinking about this one. I guess if I have 43 parameters I would need to breakup into multiple temp classes just to pump the parameters into Person.

The copy option is also interesting, cryptic but a lot less typing.

Coming from java I was hoping for some clever tricks from scala.

3 Answers 3

10

Case classes have a pre-defined copy method which you should use for this.

case class Person(name: String, age: Int)

val mike = Person("Mike", 42)

val newMike = mike.copy(age = 43)

How does this work? copy is just one of the methods (besides equals, hashCode etc) that the compiler writes for you. In this example it is:

def copy(name: String = name, age: Int = age): Person = new Person(name, age)

The values name and age in this method shadow the values in the outer scope. As you can see, default values are provided, so you only need to specify the ones that you want to change. The others default to what there are in the current instance.

1
5

The reason for the existence of var in scala is to support mutable state. In some cases, mutable state is truly what you want (e.g. for performance or clarity reasons).

You are correct, though, that there is much evidence and experience behind the encouragement to use immutable state. Things work better on many fronts (concurrency, clarity of reason, etc).

One answer to your question is to provide mutator methods to the class in question that don't actually mutate the state, but instead return a new object with a modified entry:

case class Person(val name : String, val address : String) {
   def withName(n : String) = Person(n, address)
   ...
}

This particular solution does involve coding potentially long parameter lists, but only within the class itself. Users of it get off easy:

val p = Person("Joe", "N St")
val p2 = p.withName("Sam")
...

If you consider the reasons you'd want to mutate state, then thing become clearer. If you are reading data from a database, you could have many reasons for mutating an object:

  • The database itself changed, and you want to auto-refresh the state of the object in memory
  • You want to make an update to the database itself
  • You want to pass an object around and have it mutated by methods all over the place

In the first case, immutable state is easy:

val updatedObj = oldObj.refresh

The second is much more complex, and there are many ways to handle it (including mutable state with dirty field tracking). It pays to look at libraries like Squery, where you can write things in a nice DSL (see http://squeryl.org/inserts-updates-delete.html) and avoid using the direct object mutation altogether.

The final one is the one you generally want to avoid for reasons of complexity. Such things are hard to parallelize, hard to reason about, and lead to all sorts of bugs where one class has a reference to another, but no guarantees about the stability of it. This kind of usage is the one that screams for immutable state of the form we are talking about.

1
0

Scala has adopted many paradigms from Functional Programming, one of them being a focus on using objects with immutable state. This means moving away from getters and setters within your classes and instead opting to to do what @Tony K. above has suggested: when you need to change the "state" of an inner object, define a function that will return a new Person object.

Trying to use immutable objects is likely the preferred Scala way.

In regards to the 22 parameter issue, you could create a context class that is passed to the constructor of Person:

case class PersonContext(all: String, of: String, your: String, parameters: Int)
class Person(context: PersonContext) extends PersonInfo { ... }

If you find yourself changing an address often and don't want to have to go through the PersonContext rigamarole, you can define a method:

def addressChanger(person: Person, address: String): Person = {
    val contextWithNewAddress = ...
    Person(contextWithNewAddress)
}

You could take this even further, and define a method on Person:

class Person(context: PersonContext) extends PersonInfo {
    ...
    def newAddress(address: String): Person = {
        addressChanger(this, address)
    }
}

In your code, you just need to make remember that when you are updating your objects that you're often getting new objects in return. Once you get used to that concept, it becomes very natural.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.