- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Actors and Case statement methods
Sat, 2010-10-09, 18:35
I was wondering if someone could explain what's going on language-wise with methods like this:
val fussyActor = actor {
loop {
receive {
case s: String => println("I got a String: " + s)
case i: Int => println("I got an Int: " + i.toString)
case _ => println("I have no idea what I just got.")
}
}
}
I want to know how it actually works. I've groked that the "actor" method is really a factory method for creating an "Actor" object around the stuff in the the block. What I'm wondering though is what, in scala is facilitating the use of the case statements inside of the receive method? It's using an implicit reference against an incoming parameter (i.e. the message it is sent) to use in matching a Class type.
It's like it's really wrapping my receive method's body with: _ match { /*receive body*/ } Can someone point me to where this "magic" takes place? Is it the Actor framework that's facilitating this feature, or Scala itself? (if I'm making any sense).
Thanks,
Craig.
--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: craiger
Sat, 2010-10-09, 18:47
#2
Re: Actors and Case statement methods
"receive" is a method that takes a PartialFunction[Any, Unit] as an argument. Scala turns the case statements into a PartialFunction object, which are passed into the receive method. You can look at the Scaladoc for PartialFunction to see how it works.
On Sat, Oct 9, 2010 at 1:35 PM, Craig Tataryn <craiger@tataryn.net> wrote:
--
http://erikengbrecht.blogspot.com/
On Sat, Oct 9, 2010 at 1:35 PM, Craig Tataryn <craiger@tataryn.net> wrote:
I was wondering if someone could explain what's going on language-wise with methods like this:
val fussyActor = actor {
loop {
receive {
case s: String => println("I got a String: " + s)
case i: Int => println("I got an Int: " + i.toString)
case _ => println("I have no idea what I just got.")
}
}
}
I want to know how it actually works. I've groked that the "actor" method is really a factory method for creating an "Actor" object around the stuff in the the block. What I'm wondering though is what, in scala is facilitating the use of the case statements inside of the receive method? It's using an implicit reference against an incoming parameter (i.e. the message it is sent) to use in matching a Class type.
It's like it's really wrapping my receive method's body with: _ match { /*receive body*/ } Can someone point me to where this "magic" takes place? Is it the Actor framework that's facilitating this feature, or Scala itself? (if I'm making any sense).
Thanks,
Craig.
--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: craiger
--
http://erikengbrecht.blogspot.com/
Sat, 2010-10-09, 18:57
#3
Re: Actors and Case statement methods
the magic is that your case-lines are converted into a partialfunction
which is then given to the method "receive" as a parameter. or something
like that. never looked at how actors work.
take a look at traversable.collect, it's exactly the same
Am 09.10.2010 19:35, schrieb Craig Tataryn:
> I was wondering if someone could explain what's going on language-wise with methods like this:
>
>
> val fussyActor = actor {
> loop {
> receive {
> case s: String => println("I got a String: " + s)
> case i: Int => println("I got an Int: " + i.toString)
> case _ => println("I have no idea what I just got.")
> }
> }
> }
>
>
> I want to know how it actually works. I've groked that the "actor" method is really a factory method for creating an "Actor" object around the stuff in the the block. What I'm wondering though is what, in scala is facilitating the use of the case statements inside of the receive method? It's using an implicit reference against an incoming parameter (i.e. the message it is sent) to use in matching a Class type.
>
> It's like it's really wrapping my receive method's body with: _ match { /*receive body*/ } Can someone point me to where this "magic" takes place? Is it the Actor framework that's facilitating this feature, or Scala itself? (if I'm making any sense).
>
> Thanks,
>
> Craig.
>
>
> --
> Craig Tataryn
> site: http://www.basementcoders.com/
> podcast: http://www.basementcoders.com/?feed=podcast
> itunes: http://itunes.apple.com/podcast/the-basement-coders
> irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
> twitter: craiger
>
>
Sat, 2010-10-09, 20:07
#4
Re: Actors and Case statement methods
Thanks a lot folks, I'll take a look. Sometimes I think one of my
biggest flaws is that I need to know how things work :)
Craig.
On Sat, Oct 9, 2010 at 12:43 PM, Erik Engbrecht
wrote:
> "receive" is a method that takes a PartialFunction[Any, Unit] as an
> argument. Scala turns the case statements into a PartialFunction object,
> which are passed into the receive method. You can look at the Scaladoc for
> PartialFunction to see how it works.
>
> On Sat, Oct 9, 2010 at 1:35 PM, Craig Tataryn wrote:
>>
>> I was wondering if someone could explain what's going on language-wise
>> with methods like this:
>>
>>
>> val fussyActor = actor {
>> loop {
>> receive {
>> case s: String => println("I got a String: " + s)
>> case i: Int => println("I got an Int: " + i.toString)
>> case _ => println("I have no idea what I just got.")
>> }
>> }
>> }
>>
>>
>> I want to know how it actually works. I've groked that the "actor" method
>> is really a factory method for creating an "Actor" object around the stuff
>> in the the block. What I'm wondering though is what, in scala is
>> facilitating the use of the case statements inside of the receive method?
>> It's using an implicit reference against an incoming parameter (i.e. the
>> message it is sent) to use in matching a Class type.
>>
>> It's like it's really wrapping my receive method's body with: _ match {
>> /*receive body*/ } Can someone point me to where this "magic" takes place?
>> Is it the Actor framework that's facilitating this feature, or Scala
>> itself? (if I'm making any sense).
>>
>> Thanks,
>>
>> Craig.
>>
>>
>> --
>> Craig Tataryn
>> site: http://www.basementcoders.com/
>> podcast: http://www.basementcoders.com/?feed=podcast
>> itunes: http://itunes.apple.com/podcast/the-basement-coders
>> irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
>> twitter: craiger
>>
>
>
>
> --
> http://erikengbrecht.blogspot.com/
>
val f: Int => Int = { case even if even % 2 == 0 => even case odd => odd * 2}
Likewise, if the expected parameter of a function is a function or partial function, then you can pass a block of case statements, and it will be taken as a function literal.
On Sat, Oct 9, 2010 at 14:35, Craig Tataryn <craiger@tataryn.net> wrote:
--
Daniel C. Sobral
I travel to the future all the time.