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

case objects and pattern matching

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

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

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: case objects and pattern matching
Why not just:
def act() = {
   loop {
     react {
       case s: Information => receive(s)
       case Delay(time) => Thread.sleep(time)
     }
     send()
   }
}

On Monday, 19 December 2011, 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


gabriel
Joined: 2011-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: case objects and pattern matching

yep, that would work, but I have many different group of messages and
I thought the code would be more readable this way. I would also like
to understand why these does not work. It seems that the type
inference has set s to be ControlMessages and that is why I get the
type mismatch error.

On Mon, Dec 19, 2011 at 4:09 PM, Kevin Wright wrote:
> Why not just:
>
> def act() = {
>    loop {
>      react {
>        case s: Information => receive(s)
>        case Delay(time) => Thread.sleep(time)
>      }
>      send()
>    }
> }
>
> On Monday, 19 December 2011, 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

Perhaps, simply a tipo somewhere?

> case Delay(time) =>
> Thread.sleep(time)
> ...
> 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)
^^^^^^^^^^^^
Why no parameter in Delay here?

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