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

Actors and Case statement methods

4 replies
Craig Tataryn
Joined: 2010-10-08,
User offline. Last seen 1 year 38 weeks ago.

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

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Actors and Case statement methods
This is pretty easy. A block of case statements can stand for a function or partial function literal. For instance, this works:
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:
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




--
Daniel C. Sobral

I travel to the future all the time.
Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
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:
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/
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
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
>
>

Craig Tataryn
Joined: 2010-10-08,
User offline. Last seen 1 year 38 weeks ago.
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/
>

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