This page is no longer maintained — Please continue to the home page at www.scala-lang.org

what's the usual way for implementing "updated"-methods?

5 replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

when working with immutable data structures, for example
class Person(val name:String, val age:Int)

to implement a "setter", the usual way is to implement a method like
"def cloneButWithNewName(name:String)" which creates a new instance of person, but with a new name. at least i assume it's the usual way.

is there a better way? if i have really complicated classes with lots of vals, this seems like something scala should be able to do out of the box.

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: what's the usual way for implementing "updated"-methods?

scala> case class Person(name: String, age: Int)
defined class Person

scala> Person("bob", 12).copy(age = 13)
res0: Person = Person(bob,13)

On Tue, Oct 26, 2010 at 3:52 PM, Dennis Haupt wrote:
> when working with immutable data structures, for example
> class Person(val name:String, val age:Int)
>
> to implement a "setter", the usual way is to implement a method like
> "def cloneButWithNewName(name:String)" which creates a new instance of person, but with a new name. at least i assume it's the usual way.
>
> is there a better way? if i have really complicated classes with lots of vals, this seems like something scala should be able to do out of the box.
> --
> GMX DSL Doppel-Flat ab 19,99 €/mtl.! Jetzt auch mit
> gratis Notebook-Flat! http://portal.gmx.net/de/go/dsl
>

Mirko Stocker
Joined: 2009-09-10,
User offline. Last seen 45 weeks 6 days ago.
Re: what's the usual way for implementing "updated"-methods?

On Tuesday 26 October 2010 15:52:10 Dennis Haupt wrote:
> is there a better way?

Yes! At least if you're using Scala 2.8.. case classes have a copy method:

case class Person(name:String, age:Int)
...
person.copy(name="New Name") // keeps age

more about this new feature here: http://www.scala-lang.org/node/2075

Cheers

Mirko

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: what's the usual way for implementing "updated"-methods?
class Person(val name: String, val age: Int) {
  def copy(name: String, age: Int) = new Person(name = name, age = age)
}

new Person("John", 31).copy(age = 32)


On Tue, Oct 26, 2010 at 11:52, Dennis Haupt <h-star@gmx.de> wrote:
when working with immutable data structures, for example
class Person(val name:String, val age:Int)

to implement a "setter", the usual way is to implement a method like
"def cloneButWithNewName(name:String)" which creates a new instance of person, but with a new name. at least i assume it's the usual way.

is there a better way? if i have really complicated classes with lots of vals, this seems like something scala should be able to do out of the box.
--
GMX DSL Doppel-Flat ab 19,99 &euro;/mtl.! Jetzt auch mit
gratis Notebook-Flat! http://portal.gmx.net/de/go/dsl



--
Daniel C. Sobral

I travel to the future all the time.
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: what's the usual way for implementing "updated"-methods?
Are those supposed to have defaults?

On Wed, Oct 27, 2010 at 4:21 PM, Daniel Sobral <dcsobral@gmail.com> wrote:
def copy(name: String, age: Int) = new Person(name = name, age = age)

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: what's the usual way for implementing "updated"-methods?
I guess Daniel meant  def copy(name: String = name, age: Int = age) = new Person(name, age)

On Wed, Oct 27, 2010 at 23:33, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Are those supposed to have defaults?

On Wed, Oct 27, 2010 at 4:21 PM, Daniel Sobral <dcsobral@gmail.com> wrote:
def copy(name: String, age: Int) = new Person(name = name, age = age)


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland