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

Clone with changes?

7 replies
aj
Joined: 2008-08-20,
User offline. Last seen 4 years 9 weeks ago.

Is there a supported or standard way in Scala to take an object, preferably
immutable, and clone it with modifications?

e.g. say I had

class X(val a:Int, val b:Int)

I'd like to be able to write something like

val p = new X(1,2)
val q = p with {a=2}

would be simmilar to saying
q = p.clone()
q.a = 2

Similarly with nested objects

class Y(c:Int, d:X)

val r = new Y(10,new X(1,2))
val s = r with {d = d with {a=2}}

thanks
alex

Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Clone with changes?
There was a discussion about it... 
Many agreed that with default/named it can be very nicely done, especially if compiler automagically implements those cloneAndMutate methods at least for case classes.
Szymon
On Sat, Mar 28, 2009 at 1:05 AM, Alex Jonas <alexpublic@jonases.com> wrote:

Is there a supported or standard way in Scala to take an object, preferably
immutable, and clone it with modifications?

e.g. say I had

class X(val a:Int, val b:Int)

I'd like to be able to write something like

val p = new X(1,2)
val q = p with {a=2}

would be simmilar to saying
q = p.clone()
q.a = 2

Similarly with nested objects

class Y(c:Int, d:X)

val r = new Y(10,new X(1,2))
val s = r with {d = d with {a=2}}

thanks
alex


--
View this message in context: http://www.nabble.com/Clone-with-changes--tp22752145p22752145.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Clone with changes?
Alex,

You save a character and have a slightly closer rendering to English if you substitute 'but' for 'with'.

class Y(c:Int, d:X)

val r = new Y(10,new X(1,2))
val s = r but {d = d but {a=2}}

Flipside: you add a keyword.

Best wishes,

--greg

On Fri, Mar 27, 2009 at 5:26 PM, Szymon Jachim <sjachim@gmail.com> wrote:
There was a discussion about it... 
Many agreed that with default/named it can be very nicely done, especially if compiler automagically implements those cloneAndMutate methods at least for case classes.
Szymon
On Sat, Mar 28, 2009 at 1:05 AM, Alex Jonas <alexpublic@jonases.com> wrote:

Is there a supported or standard way in Scala to take an object, preferably
immutable, and clone it with modifications?

e.g. say I had

class X(val a:Int, val b:Int)

I'd like to be able to write something like

val p = new X(1,2)
val q = p with {a=2}

would be simmilar to saying
q = p.clone()
q.a = 2

Similarly with nested objects

class Y(c:Int, d:X)

val r = new Y(10,new X(1,2))
val s = r with {d = d with {a=2}}

thanks
alex


--
View this message in context: http://www.nabble.com/Clone-with-changes--tp22752145p22752145.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ



--
L.G. Meredith
Managing Partner
Biosimilarity LLC
806 55th St NE
Seattle, WA 98105

+1 206.650.3740

http://biosimilarity.blogspot.com
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Clone with changes?

can you elaborate on this? i understand default / named is for scala
2.8.... where is documentation about 2.8? i see that the nightly
eclipse plug-in comes with the prerelease version of 2.8, so can i
use default / named already with that nightly? if so, how?

thanks, -sciss-

Am 28.03.2009 um 01:26 schrieb Szymon Jachim:

> There was a discussion about it...
>
> Many agreed that with default/named it can be very nicely done,
> especially if compiler automagically implements those
> cloneAndMutate methods at least for case classes.
>
> Szymon
>
> On Sat, Mar 28, 2009 at 1:05 AM, Alex Jonas
> wrote:
>
> Is there a supported or standard way in Scala to take an object,
> preferably
> immutable, and clone it with modifications?
>
> e.g. say I had
>
> class X(val a:Int, val b:Int)
>
> I'd like to be able to write something like
>
> val p = new X(1,2)
> val q = p with {a=2}
>
> would be simmilar to saying
> q = p.clone()
> q.a = 2
>
> Similarly with nested objects
>
> class Y(c:Int, d:X)
>
> val r = new Y(10,new X(1,2))
> val s = r with {d = d with {a=2}}
>
> thanks
> alex
>
>
> --
> View this message in context: http://www.nabble.com/Clone-with-
> changes--tp22752145p22752145.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>
>
>

Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Clone with changes?
I was talking about default / named _parameters_ and AFAIK so far it's only a SIP...
The proposed syntax (in the discussion here) for automagically generated method would be something like:
//-------------case class ACaseClass(a: Int, b: Int, c: Int)
val n = ACaseClass(1, 2, 3)
val m = n.copy(b = 4) // default for a is 1, b is 2, c is 3 
//--------------
So copy( ) is a compiler generated method that just creates another copy of the class, but with some of the values different than in original class.
When I think of it now I'm not sure if it will be possible to get this with default/named params. Because it would require the default parameters values to depend on runtime value of original class. I don't remember what SIP says about it, but in most languages the default values of parameters are compile time constants.
Szymon
On Sun, Mar 29, 2009 at 2:39 AM, Sciss <contact@sciss.de> wrote:
can you elaborate on this? i understand default / named is for scala 2.8.... where is documentation about 2.8? i see that the nightly eclipse plug-in comes with the prerelease version of 2.8, so can i use default / named already with that nightly? if so, how?

thanks, -sciss-


Am 28.03.2009 um 01:26 schrieb Szymon Jachim:

There was a discussion about it...

Many agreed that with default/named it can be very nicely done, especially if compiler automagically implements those cloneAndMutate methods at least for case classes.

Szymon

On Sat, Mar 28, 2009 at 1:05 AM, Alex Jonas <alexpublic@jonases.com> wrote:

Is there a supported or standard way in Scala to take an object, preferably
immutable, and clone it with modifications?

e.g. say I had

class X(val a:Int, val b:Int)

I'd like to be able to write something like

val p = new X(1,2)
val q = p with {a=2}

would be simmilar to saying
q = p.clone()
q.a = 2

Similarly with nested objects

class Y(c:Int, d:X)

val r = new Y(10,new X(1,2))
val s = r with {d = d with {a=2}}

thanks
alex


--
View this message in context: http://www.nabble.com/Clone-with-changes--tp22752145p22752145.html
Sent from the Scala - User mailing list archive at Nabble.com.




Rich Dougherty
Joined: 2008-08-20,
User offline. Last seen 2 years 38 weeks ago.
Re: Clone with changes?

On Sun, Mar 29, 2009 at 3:00 PM, Szymon Jachim wrote:
> When I think of it now I'm not sure if it will be possible to get this with
> default/named params. Because it would require the default parameters values
> to depend on runtime value of original class. I don't remember what SIP says
> about it, but in most languages the default values of parameters are compile
> time constants.

The current proposal says that "Default argument expressions are
re-evaluated each time a default argument is used in a method
application."

https://lampsvn.epfl.ch/trac/scala/browser/lamp-sip/named-args/sip.xhtml

Cheers
Rich

--
http://blog.richdougherty.com/

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Clone with changes?

wow, that would be amazing. already compile time constants would be
very useful, but to have expressions would be extremely nice. as i
understand i could have something like

class Node( s: Server = Servers.default, id: Int = s.nextNodeID ) ...

right? is that what is going to happen for scala 2.8?

ciao, -sciss-

Am 29.03.2009 um 05:14 schrieb Rich Dougherty:

> On Sun, Mar 29, 2009 at 3:00 PM, Szymon Jachim
> wrote:
>> When I think of it now I'm not sure if it will be possible to get
>> this with
>> default/named params. Because it would require the default
>> parameters values
>> to depend on runtime value of original class. I don't remember
>> what SIP says
>> about it, but in most languages the default values of parameters
>> are compile
>> time constants.
>
> The current proposal says that "Default argument expressions are
> re-evaluated each time a default argument is used in a method
> application."
>
> https://lampsvn.epfl.ch/trac/scala/browser/lamp-sip/named-args/
> sip.xhtml
>
> Cheers
> Rich
>
> --
> http://blog.richdougherty.com/

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Clone with changes?

On Sun, Mar 29, 2009 at 5:08 PM, Sciss wrote:
> wow, that would be amazing. already compile time constants would be very
> useful, but to have expressions would be extremely nice. as i understand i
> could have something like
>
> class Node( s: Server = Servers.default, id: Int = s.nextNodeID ) ...
>
> right? is that what is going to happen for scala 2.8?
>
Yes, that's the plan.

Cheers

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