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

"clone and subclass"

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

this might sound a little weird at first, but i came across a situation where the most simple solution would be to make a clone of a given instance but override one method or value with another.

let's say at the beginning, i create an instance of A, let's call it IOA (instance of A). a itself contains a set of algorithms that is used by A to give the result i need.
now IOA is on it's way through my system. at some point in the future, i get additional information and know that one of the contained algorithms is no longer the best one. i need to replace it.

so i thought that what i need to do is this:
"IOA.clone with TraitWithBetterAlgorithm"

since each algorithm is represented by a protected def or protected val, it would not be a problem to create a new IOA using "new A with TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original IOA.

i solved this problem by separating the configuration from the algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would have been a lot simpler.
is there a way to do it?

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: "clone and subclass"
Yay! Dynamic mixins :)
This one comes up on the mailing lists every couple of months or so.  It's not part of core Scala, but I do have a plugin in the incubator:
http://www.artima.com/weblogs/viewpost.jsp?thread=275135 http://www.artima.com/weblogs/viewpost.jsp?thread=275588http://github.com/scala-incubator/autoproxy-plugin http://wiki.github.com/scala-incubator/autoproxy-plugin
Other priorities have diverted my attention, so I've not committed any changes there for a while, but you can expect an update or two in the same timescale as the 2.8 release.


On 6 July 2010 12:51, Dennis Haupt <h-star@gmx.de> wrote:
this might sound a little weird at first, but i came across a situation where the most simple solution would be to make a clone of a given instance but override one method or value with another.

let's say at the beginning, i create an instance of A, let's call it IOA (instance of A). a itself contains a set of algorithms that is used by A to give the result i need.
now IOA is on it's way through my system. at some point in the future, i get additional information and know that one of the contained algorithms is no longer the best one. i need to replace it.

so i thought that what i need to do is this:
"IOA.clone with TraitWithBetterAlgorithm"

since each algorithm is represented by a protected def or protected val, it would not be a problem to create a new IOA using "new A with TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original IOA.

i solved this problem by separating the configuration from the algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would have been a lot simpler.
is there a way to do it?
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01



--
Kevin Wright

mail/google talk: kev.lee.wright@gmail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda

Ruediger Keller
Joined: 2010-04-11,
User offline. Last seen 42 years 45 weeks ago.
Re: "clone and subclass"

To me that sounds quite complicated for such a simple problem. Why
don't you use the strategy pattern?

http://en.wikipedia.org/wiki/Strategy_pattern

Great, there's even an example in Scala!

Btw. don't you know that clone is evil? ;-)

Cheers,
Ruediger

2010/7/6 Dennis Haupt :
> this might sound a little weird at first, but i came across a situation where the most simple solution would be to make a clone of a given instance but override one method or value with another.
>
> let's say at the beginning, i create an instance of A, let's call it IOA (instance of A). a itself contains a set of algorithms that is used by A to give the result i need.
> now IOA is on it's way through my system. at some point in the future, i get additional information and know that one of the contained algorithms is no longer the best one. i need to replace it.
>
> so i thought that what i need to do is this:
> "IOA.clone with TraitWithBetterAlgorithm"
>
> since each algorithm is represented by a protected def or protected val, it would not be a problem to create a new IOA using "new A with TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original IOA.
>
> i solved this problem by separating the configuration from the algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would have been a lot simpler.
> is there a way to do it?
> --
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>

Daniel Degrandi
Joined: 2010-05-10,
User offline. Last seen 42 years 45 weeks ago.
Re: "clone and subclass"

Ruediger Keller schrieb:
> To me that sounds quite complicated for such a simple problem. Why
> don't you use the strategy pattern?
>
> http://en.wikipedia.org/wiki/Strategy_pattern
>
> Great, there's even an example in Scala!
>
> Btw. don't you know that clone is evil? ;-)
>

Why is clone evil?

Dan

> Cheers,
> Ruediger
>
>
> 2010/7/6 Dennis Haupt :
>
>> this might sound a little weird at first, but i came across a situation where the most simple solution would be to make a clone of a given instance but override one method or value with another.
>>
>> let's say at the beginning, i create an instance of A, let's call it IOA (instance of A). a itself contains a set of algorithms that is used by A to give the result i need.
>> now IOA is on it's way through my system. at some point in the future, i get additional information and know that one of the contained algorithms is no longer the best one. i need to replace it.
>>
>> so i thought that what i need to do is this:
>> "IOA.clone with TraitWithBetterAlgorithm"
>>
>> since each algorithm is represented by a protected def or protected val, it would not be a problem to create a new IOA using "new A with TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original IOA.
>>
>> i solved this problem by separating the configuration from the algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would have been a lot simpler.
>> is there a way to do it?
>> --
>> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
>> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>>
>>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: "clone and subclass"

the strategy pattern would require me to implement A like this:
class A {

private def evalX = sometraitvar.doIt
private def evalX2 = sometraitvar2.doIt
private def evalX3 = sometraitvar3.doIt

def replaceTraitA(a:sometrait) //for each trait a method
...
}

in short: delegate each call to an exchangeable member

just using a standard implementation and use my magic "clone with" seems simpler to me.

-------- Original-Nachricht --------
> Datum: Tue, 6 Jul 2010 14:17:07 +0200
> Von: Ruediger Keller
> An: Dennis Haupt
> CC: scala-user@listes.epfl.ch
> Betreff: Re: [scala-user] "clone and subclass"

> To me that sounds quite complicated for such a simple problem. Why
> don't you use the strategy pattern?
>
> http://en.wikipedia.org/wiki/Strategy_pattern
>
> Great, there's even an example in Scala!
>
> Btw. don't you know that clone is evil? ;-)
>
> Cheers,
> Ruediger
>
>
> 2010/7/6 Dennis Haupt :
> > this might sound a little weird at first, but i came across a situation
> where the most simple solution would be to make a clone of a given instance
> but override one method or value with another.
> >
> > let's say at the beginning, i create an instance of A, let's call it IOA
> (instance of A). a itself contains a set of algorithms that is used by A
> to give the result i need.
> > now IOA is on it's way through my system. at some point in the future, i
> get additional information and know that one of the contained algorithms
> is no longer the best one. i need to replace it.
> >
> > so i thought that what i need to do is this:
> > "IOA.clone with TraitWithBetterAlgorithm"
> >
> > since each algorithm is represented by a protected def or protected val,
> it would not be a problem to create a new IOA using "new A with
> TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original
> IOA.
> >
> > i solved this problem by separating the configuration from the
> algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would
> have been a lot simpler.
> > is there a way to do it?
> > --
> > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> >

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: "clone and subclass"

it only is if you don't know what exactly it's returning
-------- Original-Nachricht --------
> Datum: Tue, 06 Jul 2010 14:22:59 +0200
> Von: Daniel Degrandi
> An: Ruediger Keller
> CC: Dennis Haupt , scala-user@listes.epfl.ch
> Betreff: Re: [scala-user] "clone and subclass"

>
> Ruediger Keller schrieb:
> > To me that sounds quite complicated for such a simple problem. Why
> > don't you use the strategy pattern?
> >
> > http://en.wikipedia.org/wiki/Strategy_pattern
> >
> > Great, there's even an example in Scala!
> >
> > Btw. don't you know that clone is evil? ;-)
> >
>
> Why is clone evil?
>
> Dan
>
> > Cheers,
> > Ruediger
> >
> >
> > 2010/7/6 Dennis Haupt :
> >
> >> this might sound a little weird at first, but i came across a situation
> where the most simple solution would be to make a clone of a given
> instance but override one method or value with another.
> >>
> >> let's say at the beginning, i create an instance of A, let's call it
> IOA (instance of A). a itself contains a set of algorithms that is used by A
> to give the result i need.
> >> now IOA is on it's way through my system. at some point in the future,
> i get additional information and know that one of the contained algorithms
> is no longer the best one. i need to replace it.
> >>
> >> so i thought that what i need to do is this:
> >> "IOA.clone with TraitWithBetterAlgorithm"
> >>
> >> since each algorithm is represented by a protected def or protected
> val, it would not be a problem to create a new IOA using "new A with
> TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original
> IOA.
> >>
> >> i solved this problem by separating the configuration from the
> algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would
> have been a lot simpler.
> >> is there a way to do it?
> >> --
> >> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> >> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> >>
> >>

Ruediger Keller
Joined: 2010-04-11,
User offline. Last seen 42 years 45 weeks ago.
Re: "clone and subclass"

I suggest reading Joshua Bloch's book "Effective Java: Programming
Language Guide". But I quickly found this via Google and I think it
also explains to some extend why clone is evil:

http://www.javapractices.com/topic/TopicAction.do?Id=71
or perhaps
http://en.wikipedia.org/wiki/Clone_%28Java_method%29

As to your problem. It seems you A is just a container for various
interchangeble algorithms. If you used a case class you could
something like the following:

type MyAlgorithm = MyData => Unit
case class A(algo1: MyAlgorithm, algo2: MyAlgorithm, algo3: MyAlgorithm)
val ioa = A(Algorithm1, Algorithm2, Algorithm3)
val iob2 = ioa.copy(algo1 = Algorithm4)

Cheers,
Ruediger

2010/7/6 Daniel Degrandi :
>
> Ruediger Keller schrieb:
>>
>> To me that sounds quite complicated for such a simple problem. Why
>> don't you use the strategy pattern?
>>
>> http://en.wikipedia.org/wiki/Strategy_pattern
>>
>> Great, there's even an example in Scala!
>>
>> Btw. don't you know that clone is evil? ;-)
>>
>
> Why is clone evil?
>
> Dan
>
>> Cheers,
>> Ruediger
>>
>>
>> 2010/7/6 Dennis Haupt :
>>
>>>
>>> this might sound a little weird at first, but i came across a situation
>>> where the most simple solution would be to make a clone of a given instance
>>> but override one method or value with another.
>>>
>>> let's say at the beginning, i create an instance of A, let's call it IOA
>>> (instance of A). a itself contains a set of algorithms that is used by A to
>>> give the result i need.
>>> now IOA is on it's way through my system. at some point in the future, i
>>> get additional information and know that one of the contained algorithms is
>>> no longer the best one. i need to replace it.
>>>
>>> so i thought that what i need to do is this:
>>> "IOA.clone with TraitWithBetterAlgorithm"
>>>
>>> since each algorithm is represented by a protected def or protected val,
>>> it would not be a problem to create a new IOA using "new A with
>>> TraitWithBetterAlgorithm" - but this way, i loose the configuation of the
>>> original IOA.
>>>
>>> i solved this problem by separating the configuration from the algorithms
>>> so i can create new IOAs as i need them, but "IOA.clone with x" would have
>>> been a lot simpler.
>>> is there a way to do it?
>>> --
>>> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
>>> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>>>
>>>
>

Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: "clone and subclass"

  Could you not use a class where every method that you want
to "dynamically override" is a closure, then in you clone you could reassign the closures ?

Max

On Tue, Jul 6, 2010 at 7:51 AM, Dennis Haupt <h-star@gmx.de> wrote:
this might sound a little weird at first, but i came across a situation where the most simple solution would be to make a clone of a given instance but override one method or value with another.

let's say at the beginning, i create an instance of A, let's call it IOA (instance of A). a itself contains a set of algorithms that is used by A to give the result i need.
now IOA is on it's way through my system. at some point in the future, i get additional information and know that one of the contained algorithms is no longer the best one. i need to replace it.

so i thought that what i need to do is this:
"IOA.clone with TraitWithBetterAlgorithm"

since each algorithm is represented by a protected def or protected val, it would not be a problem to create a new IOA using "new A with TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original IOA.

i solved this problem by separating the configuration from the algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would have been a lot simpler.
is there a way to do it?
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: "clone and subclass"

yes
-------- Original-Nachricht --------
> Datum: Tue, 6 Jul 2010 08:51:36 -0400
> Von: "Maxime Lévesque"
> An: Dennis Haupt
> CC: scala-user@listes.epfl.ch
> Betreff: Re: [scala-user] "clone and subclass"

> Could you not use a class where every method that you want
> to "dynamically override" is a closure, then in you clone you could
> reassign
> the closures ?
>
> Max
>
> On Tue, Jul 6, 2010 at 7:51 AM, Dennis Haupt wrote:
>
> > this might sound a little weird at first, but i came across a situation
> > where the most simple solution would be to make a clone of a given
> instance
> > but override one method or value with another.
> >
> > let's say at the beginning, i create an instance of A, let's call it IOA
> > (instance of A). a itself contains a set of algorithms that is used by A
> to
> > give the result i need.
> > now IOA is on it's way through my system. at some point in the future, i
> > get additional information and know that one of the contained algorithms
> is
> > no longer the best one. i need to replace it.
> >
> > so i thought that what i need to do is this:
> > "IOA.clone with TraitWithBetterAlgorithm"
> >
> > since each algorithm is represented by a protected def or protected val,
> it
> > would not be a problem to create a new IOA using "new A with
> > TraitWithBetterAlgorithm" - but this way, i loose the configuation of
> the
> > original IOA.
> >
> > i solved this problem by separating the configuration from the
> algorithms
> > so i can create new IOAs as i need them, but "IOA.clone with x" would
> have
> > been a lot simpler.
> > is there a way to do it?
> > --
> > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> >

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: "clone and subclass"
You've never heard of evil clones?

On 6 July 2010 13:22, Daniel Degrandi <Daniel.Degrandi@uni-duesseldorf.de> wrote:

Ruediger Keller schrieb:
To me that sounds quite complicated for such a simple problem. Why
don't you use the strategy pattern?

http://en.wikipedia.org/wiki/Strategy_pattern

Great, there's even an example in Scala!

Btw. don't you know that clone is evil? ;-)
 

Why is clone evil?

Dan

Cheers,
Ruediger


2010/7/6 Dennis Haupt <h-star@gmx.de>:
 
this might sound a little weird at first, but i came across a situation where the most simple solution would be to make a clone of a given instance but override one method or value with another.

let's say at the beginning, i create an instance of A, let's call it IOA (instance of A). a itself contains a set of algorithms that is used by A to give the result i need.
now IOA is on it's way through my system. at some point in the future, i get additional information and know that one of the contained algorithms is no longer the best one. i need to replace it.

so i thought that what i need to do is this:
"IOA.clone with TraitWithBetterAlgorithm"

since each algorithm is represented by a protected def or protected val, it would not be a problem to create a new IOA using "new A with TraitWithBetterAlgorithm" - but this way, i loose the configuation of the original IOA.

i solved this problem by separating the configuration from the algorithms so i can create new IOAs as i need them, but "IOA.clone with x" would have been a lot simpler.
is there a way to do it?
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

   



--
Kevin Wright

mail/google talk: kev.lee.wright@gmail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda

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