- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
what's the usual way for implementing "updated"-methods?
Tue, 2010-10-26, 14:52
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.
Tue, 2010-10-26, 15:17
#2
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
Wed, 2010-10-27, 21:27
#3
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:
--
Daniel C. Sobral
I travel to the future all the time.
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 €/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.
Wed, 2010-10-27, 22:37
#4
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:
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)
Thu, 2010-10-28, 07:47
#5
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:
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)
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
>