- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
cloning of scala actors?
Thu, 2009-03-19, 12:43
I've just started a project where I implement both SWT event handlers and "business logic" using scala.actors.
I'm using
val executor1 = actor { /* ... */}
to provide body and create instances of actors.
The problem is: How do I clone (same class, same body, empty mailbox, potentially new thread) an actor?
Something along the lines of like
val executor2 = clone(executor1)
or
val executor3 = executor !? Clone
would be great...
Does anyone have any ideas?
I was thinking about reflective newInstance and fields copying...
Ugly solution and probably won't work OK.
Do you know of any reasons why it might be impossible (even assuming the scala.actors.Actor will be patched)?
Szymon
I'm using
val executor1 = actor { /* ... */}
to provide body and create instances of actors.
The problem is: How do I clone (same class, same body, empty mailbox, potentially new thread) an actor?
Something along the lines of like
val executor2 = clone(executor1)
or
val executor3 = executor !? Clone
would be great...
Does anyone have any ideas?
I was thinking about reflective newInstance and fields copying...
Ugly solution and probably won't work OK.
Do you know of any reasons why it might be impossible (even assuming the scala.actors.Actor will be patched)?
Szymon
Thu, 2009-03-19, 20:57
#2
Re: cloning of scala actors?
Actually I don't care about it's state... I want a clean copy of the class and body of this actor - how it looked like just before it was started.
So after reading your email I feel it would be quite easy... and bad that I even asked my question. :-)
Anyway thanks.
Szymon
On Thu, Mar 19, 2009 at 7:32 PM, Rich Dougherty <rich@rd.gen.nz> wrote:
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
So after reading your email I feel it would be quite easy... and bad that I even asked my question. :-)
Anyway thanks.
Szymon
On Thu, Mar 19, 2009 at 7:32 PM, Rich Dougherty <rich@rd.gen.nz> wrote:
Hi Szymon
How do you want to define an actor's state? An actor may be executing some code, so its state is intertwined with the thread that's its executing within (e.g. stack, monitors, etc).
If you just mean some known set of variables, then you should probably just write code to clone the variables - sorry. The cloning would probably be a "manual" job, but you could make your life easier by holding the state in some form that's easy to clone or even immutable so it's reference can be shared by both the old and new instance. Whether this is possible depends on the specific details of your problem...
sealed trait State
case object InitialState extends State
case class SomethingState(....) extends State
def executor(state: State) = actor {
...
react {
...
case Clone => reply(executor(state))
...
}
}
val executor1 = executor(InitialState)
val executor2 = executor1 ! Clone
Cheers
Rich
On Fri, Mar 20, 2009 at 12:42 AM, Szymon Jachim <sjachim@gmail.com> wrote:
I've just started a project where I implement both SWT event handlers and "business logic" using scala.actors.
I'm using
val executor1 = actor { /* ... */}
to provide body and create instances of actors.
The problem is: How do I clone (same class, same body, empty mailbox, potentially new thread) an actor?
Something along the lines of like
val executor2 = clone(executor1)
or
val executor3 = executor !? Clone
would be great...
Does anyone have any ideas?
I was thinking about reflective newInstance and fields copying...
Ugly solution and probably won't work OK.
Do you know of any reasons why it might be impossible (even assuming the scala.actors.Actor will be patched)?
Szymon
--
http://blog.richdougherty.com/
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
Fri, 2009-03-20, 18:07
#3
Re: cloning of scala actors?
On Thu, Mar 19, 2009 at 4:42 AM, Szymon Jachim <sjachim@gmail.com> wrote:
I've just started a project where I implement both SWT event handlers and "business logic" using scala.actors.
I'm using
val executor1 = actor { /* ... */}
if you change this to:
def createAnActor = actor { /* ... */ }
you can create multiple instances of the Actor.
to provide body and create instances of actors.
The problem is: How do I clone (same class, same body, empty mailbox, potentially new thread) an actor?
Something along the lines of like
val executor2 = clone(executor1)
or
val executor3 = executor !? Clone
would be great...
Does anyone have any ideas?
I was thinking about reflective newInstance and fields copying...
Ugly solution and probably won't work OK.
Do you know of any reasons why it might be impossible (even assuming the scala.actors.Actor will be patched)?
Szymon
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
How do you want to define an actor's state? An actor may be executing some code, so its state is intertwined with the thread that's its executing within (e.g. stack, monitors, etc).
If you just mean some known set of variables, then you should probably just write code to clone the variables - sorry. The cloning would probably be a "manual" job, but you could make your life easier by holding the state in some form that's easy to clone or even immutable so it's reference can be shared by both the old and new instance. Whether this is possible depends on the specific details of your problem...
sealed trait State
case object InitialState extends State
case class SomethingState(....) extends State
def executor(state: State) = actor {
...
react {
...
case Clone => reply(executor(state))
...
}
}
val executor1 = executor(InitialState)
val executor2 = executor1 ! Clone
Cheers
Rich
On Fri, Mar 20, 2009 at 12:42 AM, Szymon Jachim <sjachim@gmail.com> wrote:
--
http://blog.richdougherty.com/