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

Re: case objects and pattern matching

1 reply
gabriel
Joined: 2011-12-19,
User offline. Last seen 42 years 45 weeks ago.

actually this is the code:

I tried to make a shorter version but is seems I failed.

/////////

package gainlo

import actors.Actor
import collection.immutable.HashSet
import scala.collection.immutable.List

/**
* Created by IntelliJ IDEA.
* User: gabriel
* Date: 11/12/11
* Time: 12:33
* To change this template use File | Settings | File Templates.
*/

trait Information{
def id()
}

abstract class ControlMessages

/**
* Messague that is send to the Nodes stating that the should stop working
*/
case class AbortSimulation extends ControlMessages

/**
* Messague that is send to the Nodes statinng that the should stop
its computation
* by the argument given by time.
*/
case class DelayInformation(time: Int) extends ControlMessages

/**
* This message is given
*/
case class DeadNode() extends ControlMessages

trait BasicUnit extends Actor{

val id:Int

def vec_iterator: Iterator[BasicUnit]

def send()

var seen_inf = new HashSet[Information]()
def receive(inf: Information) {
if(!seen_inf.contains(inf)){
seen_inf = seen_inf + inf
}
println(id + " " + inf)
}

def act() = {
loop {
react{
case s: Information => receive(s)
case s: ControlMessages => s match {
case AbortSimulation => exit()
case DelayInformation(time) =>
Thread.sleep(time)
}
}
send()
}
}
}

class Name(id_name:String) extends Information{
def id():String = {
id_name
}
}

class Node(id: Int) extends BasicUnit{

var neighs = new HashSet[BasicUnit]

def vec_iterator = neighs.iterator

def send(){
val vec = vec_iterator
while (vec.hasNext) {
val x = vec.next
x!new Name("pepe")
}
}
}

object Test extends App{
override def main(args: Array[String]) {
println("Aqui esta la papa")
val node_a = new Node(1)
val node_b = new Node(2)
node_a.start()
node_b.start()
Thread.sleep(100)

}
}

////////////////// End Code //////

On Mon, Dec 19, 2011 at 5:33 PM, Som Snytt wrote:
>
> Not sure what the code looks like, but it's complaining about an object, not
> a class:
> found   : object gainlo.Delay
>
> HTH...
>
>
> On Mon, Dec 19, 2011 at 10:43 AM, gabriel wrote:
>>
>> Hi guys, I am walking my first steps in scala, I am trying to program
>> a bit with actors and I have the following problem with which you
>> might be able to help me.
>>
>> this is the main loop of an actor.
>>
>>
>>
>> def act() = {
>>    loop {
>>      react{
>>        case s: Information => receive(s)
>>        case s: ControlMessages => s match {
>>          case Delay(time) =>
>>            Thread.sleep(time)
>>        }
>>      }
>>      send()
>>    }
>>  }
>> }
>>
>>
>> Delay is a subclass of ControlMessages. I get the following exception:
>>
>> error: pattern type is incompatible with expected type;
>> found   : object gainlo.Delay
>> required: gainlo.ControlMessages
>> case Delay => Thread.sleep(time)
>>
>> thanks for any help.
>>
>> gabriel
>>
>>
>

E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.
Re: case objects and pattern matching

Hi Gabriel,

The usage of case classes without parameters is discouraged.
(The compiler should issue a warning)

On 2011-12-20 02:52, Gabriel Infante-Lopez wrote:
> case class AbortSimulation extends ControlMessages
^^^^^^^^^^^^^^^

> case class DeadNode() extends ControlMessages

You could try to use objects instead:

object AbortSimulation extends ControlMessages
object DeadNode extends ControlMessages

I think, that the problem in your code is that the pattern match is tried against the *object*
AbortSimulation. This object does really exists: it has been automatically created by the compiler
as the companion object for the class AbortSimulation, which has been defined as a case class.

And here is the main point: the companion object is not an instance of its companion class, and
therefore, is not a subtype of the parent classes of the companion class.

Thus, the AbortSimulation object is not a subtype of ControlMessages.
Hence the error message.

Hope, this help.

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