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

Idea: Changing singe field of case class

10 replies
Stepan Koltsov
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.

Hi.

There is a problem: how to change a single field of a case class with
all fields immutable.

Mine proposal depends on named args and default values SIP.

http://lampsvn.epfl.ch/svn-repos/scala/lamp-sip/named-args/sip.xhtml

Each case class gets generated "copy" method with parameters same as
case class fields. For example:

===
case class Person(name: String, age: Int, eyesColor: String) {
// this method is generated by compiler:
def copy(name = this.name, age = this.age, eyesColor =
this.eyesColor) = new Person(name, age, eyesColor)
}

val p = new Person("Hilbert", 66, "green")
val q = p.copy(age = 67) // copy of "p" with "age" field changed to 67
===

I haven't read mailing lists some time, so sorry, if this Idea belongs
to someone else :)

S.

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Idea: Changing singe field of case class
This looks awesome.  Two thumbs up!

On Fri, Jan 9, 2009 at 10:35 AM, Stepan Koltsov <stepan [dot] koltsov [at] gmail [dot] com> wrote:
Hi.

There is a problem: how to change a single field of a case class with
all fields immutable.

Mine proposal depends on named args and default values SIP.

http://lampsvn.epfl.ch/svn-repos/scala/lamp-sip/named-args/sip.xhtml

Each case class gets generated "copy" method with parameters same as
case class fields. For example:

===
case class Person(name: String, age: Int, eyesColor: String) {
 // this method is generated by compiler:
 def copy(name = this.name, age = this.age, eyesColor =
this.eyesColor) = new Person(name, age, eyesColor)
}

val p = new Person("Hilbert", 66, "green")
val q = p.copy(age = 67)   // copy of "p" with "age" field changed to 67
===

I haven't read mailing lists some time, so sorry, if this Idea belongs
to someone else :)

S.



--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Idea: Changing singe field of case class

On Fri, Jan 9, 2009 at 6:35 PM, Stepan Koltsov wrote:
> Each case class gets generated "copy" method with parameters same as
> case class fields. For example:

Very nice!

Cheers,

Miles

Carsten Saager
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Idea: Changing singe field of case class
What about a little bit more compiler magic:

val p = new Person("Hilbert", 66, "green")
val younger = p.age=65
val even_younger = younger.age++
val older = p.birthday()

with the method

def birthday() = age++

To avoid accidental confusions with vars the compiler will warn/error "modifications" of vals if it is not a lhs expression

/Carsten

On Fri, Jan 9, 2009 at 7:35 PM, Stepan Koltsov <stepan [dot] koltsov [at] gmail [dot] com> wrote:
Hi.

There is a problem: how to change a single field of a case class with
all fields immutable.

Mine proposal depends on named args and default values SIP.

http://lampsvn.epfl.ch/svn-repos/scala/lamp-sip/named-args/sip.xhtml

Each case class gets generated "copy" method with parameters same as
case class fields. For example:

===
case class Person(name: String, age: Int, eyesColor: String) {
 // this method is generated by compiler:
 def copy(name = this.name, age = this.age, eyesColor =
this.eyesColor) = new Person(name, age, eyesColor)
}

val p = new Person("Hilbert", 66, "green")
val q = p.copy(age = 67)   // copy of "p" with "age" field changed to 67
===

I haven't read mailing lists some time, so sorry, if this Idea belongs
to someone else :)

S.

Derek Chen-Becker
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Idea: Changing singe field of case class

David Pollak wrote:
> This looks awesome. Two thumbs up!

Agreed. This definitely reduces some of the costs of using immutable
objects.

Derek

loverdos
Joined: 2008-11-18,
User offline. Last seen 2 years 27 weeks ago.
Re: Idea: Changing singe field of case class
This is really nice.
Unless we miss something not-so-obviously bad, I think it should be added as a use-case in the aforementioned SIP.

On Fri, Jan 9, 2009 at 8:35 PM, Stepan Koltsov <stepan [dot] koltsov [at] gmail [dot] com> wrote:
Hi.

There is a problem: how to change a single field of a case class with
all fields immutable.

Mine proposal depends on named args and default values SIP.

http://lampsvn.epfl.ch/svn-repos/scala/lamp-sip/named-args/sip.xhtml

Each case class gets generated "copy" method with parameters same as
case class fields. For example:

===
case class Person(name: String, age: Int, eyesColor: String) {
 // this method is generated by compiler:
 def copy(name = this.name, age = this.age, eyesColor =
this.eyesColor) = new Person(name, age, eyesColor)
}

val p = new Person("Hilbert", 66, "green")
val q = p.copy(age = 67)   // copy of "p" with "age" field changed to 67
===

I haven't read mailing lists some time, so sorry, if this Idea belongs
to someone else :)

S.



--
 __~O
-\ <,       Christos KK Loverdos
(*)/ (*)      http://ckkloverdos.com
rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: Idea: Changing singe field of case class


On Sat, Jan 10, 2009 at 17:05, Christos KK Loverdos <loverdos [at] gmail [dot] com> wrote:
This is really nice.
Unless we miss something not-so-obviously bad, I think it should be added as a use-case in the aforementioned SIP.

I'll do that, thanks to Stepan.

Lukas
Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
re : Idea: Changing singe field of case class

Hi Stephan / others,

sorry to reply out of context, this email finally pushed me to
subscribe to scala and scala-debate.

The idea has been thought of before (and derivations before I looked
at it, could have sworn something similar was on the lift site):

http://code.google.com/p/scala-scales/wiki/VirtualConstructorPreSIP

I initially attempted to make it a compiler plugin but that didn't go too well.

In essence due to type hierarchies and generics you need abstract type
members, I'm using the approach with some of my own projects and it
works well.

I'd be all for something like this hitting scala as a built in syntax
(especially with compiler generated setters / modifiers / copiers for
each field).

The reasons I didn't push this was nobody else seemed to want / need
it and I have no idea if the virtual class stuff for 2.8.0 covers some
of this.

cheers,
Chris

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: Idea: Changing singe field of case class

>>>>> "Stepan" == Stepan Koltsov writes:

Stepan> Hi. There is a problem: how to change a single field of a case
Stepan> class with all fields immutable.

Yeah, it'd be really great to have an easy way to do this.

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: re : Idea: Changing singe field of case class

Chris Twiner wrote:
> The idea has been thought of before (and derivations before I looked
> at it, could have sworn something similar was on the lift site):
>
I don't think it's related to constructors at all, but... I cooked this
up a year ago, and it didn't make much of a splash. My motivation is
purely practical--both Erlang and Haskell have a sugary way to do
property "updates" that actually return a marginally modified object;
accomplishing the same thing in Scala requires some boilerplate.

http://www.liftweb.net/index.php/Scala_Record_Update_Syntax_Proposal

-0xe1a

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: re : Idea: Changing singe field of case class

thats the one I was thinking of. The reason virtual constructors
(http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8)
comes into the discussion is highlighted in the link I gave, but
suffice to say base classes have no idea on how to create derived
classes. A virtual function call is required for that, letting the
derived class create a new correct instance of itself.

maybe c++ has damadged me and there is an obvious, functional / scala
way to do this, but I've not found it yet, and the abstract type
members (for type parameter handling) and copy / newThis approach
works great so far. With further boilerplate removal it would be even
better.

On 1/14/09, Alex Cruise wrote:
> Chris Twiner wrote:
>
> > The idea has been thought of before (and derivations before I looked
> > at it, could have sworn something similar was on the lift site):
> >
> >
> I don't think it's related to constructors at all, but... I cooked this up
> a year ago, and it didn't make much of a splash. My motivation is purely
> practical--both Erlang and Haskell have a sugary way to do property
> "updates" that actually return a marginally modified object; accomplishing
> the same thing in Scala requires some boilerplate.
>
> http://www.liftweb.net/index.php/Scala_Record_Update_Syntax_Proposal
>
> -0xe1a
>

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