What does this mean ?

196 views
Skip to first unread message

Clement Dourval

unread,
Feb 17, 2016, 9:40:20 AM2/17/16
to scala-language
Hi, I'm currently Learning Scala and more precisely, concurrent programming with scala.

I found this code :

class Pong extends Actor {
  def act() {
    var pongCount = 0
    while (true) {
      receive {
        case Ping =>
          if (pongCount % 1000 == 0)
            Console.println("Pong: ping "+pongCount)
          sender ! Pong
          pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong: stop")
          exit()
      }
    }
  }
}

This is from a tutorial, so i'm expecting it to work, the only problem is I do not understand how.

The main problem is, how does this code use the case keyword since there is no match !
(adress of the tutorial : http://www.scala-lang.org/old/node/242)

Anyway, i understood what was the tutorial was supposed to do (with ping pong), I just don't understand what is written, I'd like to be able to write it myself.
But it seems i'm missing some syntax subtleties
And even if these are deprecated, i found the same synthax with the akka Library ! So I'm asking myself how to read this.

Anyone can help ? Thanks in advance !

Oliver Ruebenacker

unread,
Feb 17, 2016, 9:57:49 AM2/17/16
to scala-l...@googlegroups.com

     Hello,

  The case keyword creates a partial function, e.g.:

scala> val pf: Int => String = { case 42 => "Hello!" }
pf: Int => String = <function1>

scala> pf(42)
res1: String = Hello!

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Clement Dourval

unread,
Feb 17, 2016, 10:11:05 AM2/17/16
to scala-language
But what does the fact that the "receive" word is not followed by the '=' sign is meaning ?
receive is suposed to be a function here right ?
is that a kind of averride without having to write the whole signature.

From where i'm standing, it seems to me that receive is a function with partial cases depending on the main argument to be Ping typed or other.

But there is no = sign so what does this mean ?

Naftoli Gugenheim

unread,
Feb 17, 2016, 10:16:08 AM2/17/16
to scala-language

It's invoking the receive method and passing it a partial function as a parameter.

This is using the obsolete actor library, you should use Akka instead.

Oliver Ruebenacker

unread,
Feb 17, 2016, 10:22:41 AM2/17/16
to scala-l...@googlegroups.com

     Hello,

  receive {...} is a call of the "receive" method and short for receive({...})

     Best, Oliver

Clement Dourval

unread,
Feb 17, 2016, 10:40:46 AM2/17/16
to scala-language
And what would the body of the receive function look like ?
If it is possible to write a quick view of it :)
I promise I will stop just after ! ^^
Message has been deleted

Clement Dourval

unread,
Feb 17, 2016, 10:53:48 AM2/17/16
to scala-language
Ok I'm begining to understand :
the "ping ! Pong" is calling the '!' method and putting a Pong in a mailbox.
And the fact that the other side is continuesly calling the receive method is kind of forcing the scan of the mailbox as much as possible.
Then when the mailbox has a message in it, it feeds the message as argument for the partial method
am i right ?

Oliver Ruebenacker

unread,
Feb 17, 2016, 10:53:59 AM2/17/16
to scala-l...@googlegroups.com

     Hello,

  I don't know the receive method's signature, but assuming you are just looking for an example of a method that takes a partial function:

scala> def m(pf: PartialFunction[Int, String]) : Seq[String] = { (1 to 100).collect(pf) }
m: (pf: PartialFunction[Int,String])Seq[String]

scala> m{case 42 => "Hello!" }
res4: Seq[String] = Vector(Hello!)

     Best, Oliver


On Wed, Feb 17, 2016 at 10:44 AM, Clement Dourval <clement...@ensc.fr> wrote:
and what would be the receive function's signature ?

Oliver Ruebenacker

unread,
Feb 17, 2016, 10:54:55 AM2/17/16
to scala-l...@googlegroups.com
sounds right.

Clement Dourval

unread,
Feb 17, 2016, 10:56:20 AM2/17/16
to scala-l...@googlegroups.com
Ok thank you very much for the help.

--
You received this message because you are subscribed to a topic in the Google Groups "scala-language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-language/CHHe5WO3f90/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-languag...@googlegroups.com.

John Michael Lafayette

unread,
Apr 10, 2016, 11:20:01 PM4/10/16
to scala-language
So the "receive" method takes in an arbitrary number of partial functions of type partialFunction[Any, A] as its parameter and it dynamically checks the input against each partial function using isDefinedAtuntil it gets a match.

But the type signature for receive is: receive[A](f: PartialFunction[Any, A]): A

It should only take in a single partial function, f, not a vararg or list or array of partial functions. Is "
case Stop => Console.println("Pong: stop") " a single partial function or is it part of a single partial function object?

 

Oliver Ruebenacker

unread,
Apr 11, 2016, 10:25:58 AM4/11/16
to scala-l...@googlegroups.com

     Hello,

  The receive method takes one partial function. A partial function can consist of multiple case statements.

scala> val pf : PartialFunction[Int, String] = { case 1 => "one"; case 2=> "two"}
pf: PartialFunction[Int,String] = <function1>

scala> (1 to 10).collect(pf)
res1: scala.collection.immutable.IndexedSeq[String] = Vector(one, two)

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages